--- android/Side9/src/dk/thoerup/side9/ImageAdapter.java 2011/01/30 21:15:55 1221 +++ android/Side9/src/dk/thoerup/side9/ImageAdapter.java 2011/02/08 07:32:07 1222 @@ -25,7 +25,8 @@ import android.widget.Toast; public class ImageAdapter extends BaseAdapter { - + final static String DIR_NAME = "/Side9"; + final static String THUMBDIR_NAME = "/.thumb/"; //int mGalleryItemBackground; @@ -38,13 +39,43 @@ 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; - String path = Environment.getExternalStorageDirectory().getPath() + "/Side9" ; - - + String path = Environment.getExternalStorageDirectory().getPath() + DIR_NAME ; + File root = new File(path); @@ -54,41 +85,14 @@ } File files[] = root.listFiles( new ExtensionFilter("jpg") ); + - - String thumbPath = path + "/.thumb/"; - - File thumbDir = new File(thumbPath); - if (! thumbDir.exists()) { - thumbDir.mkdirs(); - } - - - for (File f : files) { - + for (File f : files) { ImageEntry entry = new ImageEntry(); entry.path = f.getPath(); - String fileName = f.getName(); - File thumb = new File ( thumbPath + fileName ); - - if (! thumb.exists() || thumb.lastModified() < f.lastModified()) { - Bitmap bmp = BitmapFactory.decodeFile( f.getPath() ); - Bitmap scaled = Bitmap.createScaledBitmap(bmp, bmp.getWidth()/4, bmp.getHeight()/4, true); - entry.thumb = scaled; - try { - FileOutputStream fos = new FileOutputStream(thumb); - scaled.compress(CompressFormat.JPEG, 90, fos); - fos.close(); - - } catch (IOException e) { - Log.e("Side9", "error", e); - } - - } else { - Bitmap thumbBmp = BitmapFactory.decodeFile( thumb.getPath()); - entry.thumb = thumbBmp; - } + entry.thumb = generateAndLoadThumb(f); + /* load captions */ String infoFileName = f.getPath().replace(".jpg", ".txt");