--- android/Side9/src/dk/thoerup/side9/ImageAdapter.java 2011/02/08 07:32:07 1222 +++ android/Side9/src/dk/thoerup/side9/ImageAdapter.java 2011/02/09 07:14:53 1223 @@ -28,48 +28,14 @@ final static String DIR_NAME = "/Side9"; final static String THUMBDIR_NAME = "/.thumb/"; - - //int mGalleryItemBackground; + //int mGalleryItemBackground; private Context mContext; ArrayList mImages = new ArrayList(); - - - - int thumbSize; - public static Bitmap generateAndLoadThumb(File image) { - - File thumbDir = new File(Environment.getExternalStorageDirectory().getPath() + DIR_NAME + THUMBDIR_NAME); - String fileName = image.getName(); - File thumb = new File ( thumbDir.getPath() + "/" + fileName ); - - Bitmap thumbBmp = null; - - if (! thumbDir.exists()) { - thumbDir.mkdirs(); - } - - if (! thumb.exists() || thumb.lastModified() < image.lastModified()) { - Bitmap bmp = BitmapFactory.decodeFile( image.getPath() ); - thumbBmp = Bitmap.createScaledBitmap(bmp, bmp.getWidth()/4, bmp.getHeight()/4, true); - try { - FileOutputStream fos = new FileOutputStream(thumb); - thumbBmp.compress(CompressFormat.JPEG, 90, fos); - fos.close(); - - } catch (IOException e) { - Log.e("Side9", "error", e); - } - - } else { - thumbBmp = BitmapFactory.decodeFile( thumb.getPath()); - } - return thumbBmp; - } public ImageAdapter(Context c) { mContext = c; @@ -91,7 +57,7 @@ ImageEntry entry = new ImageEntry(); entry.path = f.getPath(); - entry.thumb = generateAndLoadThumb(f); + //entry.thumb = generateAndLoadThumb(f); /* load captions */ @@ -116,6 +82,37 @@ mImages.clear(); } + public static Bitmap generateAndLoadThumb(File image) { + + File thumbDir = new File(Environment.getExternalStorageDirectory().getPath() + DIR_NAME + THUMBDIR_NAME); + String fileName = image.getName(); + File thumb = new File ( thumbDir.getPath() + "/" + fileName ); + + Bitmap thumbBmp = null; + + if (! thumbDir.exists()) { + thumbDir.mkdirs(); + } + + if (! thumb.exists() || thumb.lastModified() < image.lastModified()) { + Bitmap bmp = BitmapFactory.decodeFile( image.getPath() ); + thumbBmp = Bitmap.createScaledBitmap(bmp, bmp.getWidth()/4, bmp.getHeight()/4, true); + + try { + FileOutputStream fos = new FileOutputStream(thumb); + thumbBmp.compress(CompressFormat.JPEG, 90, fos); + fos.close(); + + } catch (IOException e) { + Log.e("Side9", "error", e); + } + + } else { + thumbBmp = BitmapFactory.decodeFile( thumb.getPath()); + } + return thumbBmp; + } + public void orderByPath() { Collections.sort(mImages, new ImageEntry.PathComparator()); this.notifyDataSetChanged(); @@ -159,6 +156,18 @@ } public View getView(int position, View convertView, ViewGroup parent) { + + if (position >= mImages.size()) { + return null; + } + + ImageEntry currentEntry = mImages.get(position); + + if (currentEntry.thumb == null ) { + File thumb = new File ( currentEntry.path ); + currentEntry.thumb = generateAndLoadThumb(thumb); + } + ImageView imageView; if (convertView == null) { // if it's not recycled, initialize some attributes imageView = new ImageView(mContext); @@ -171,7 +180,7 @@ } //imageView.setImageURI( Uri.parse(mThumbPaths.get(position)) ); - imageView.setImageBitmap( mImages.get(position).thumb ); + imageView.setImageBitmap( currentEntry.thumb ); return imageView; }