--- android/Side9/src/dk/thoerup/side9/ImageAdapter.java 2010/06/03 09:31:00 791 +++ android/Side9/src/dk/thoerup/side9/ImageAdapter.java 2010/11/03 05:21:48 1186 @@ -1,43 +1,125 @@ package dk.thoerup.side9; import java.io.File; +import java.io.FileOutputStream; +import java.io.FilenameFilter; +import java.io.IOException; +import java.io.RandomAccessFile; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.graphics.Bitmap.CompressFormat; import android.os.Environment; +import android.util.DisplayMetrics; +import android.util.Log; import android.view.View; import android.view.ViewGroup; +import android.view.WindowManager; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; +import android.widget.Toast; public class ImageAdapter extends BaseAdapter { + + + //int mGalleryItemBackground; private Context mContext; - ArrayList mBitmapPaths = new ArrayList(); - ArrayList mBitmaps = new ArrayList(); + ArrayList mImages = new ArrayList(); + + + + + int thumbSize; public ImageAdapter(Context c) { mContext = c; String path = Environment.getExternalStorageDirectory().getPath() + "/Side9" ; + + File root = new File(path); - File files[] = root.listFiles(); - for (File f : files) { - Bitmap bmp = BitmapFactory.decodeFile( f.getPath() ); - Bitmap scaled = Bitmap.createScaledBitmap(bmp, bmp.getWidth()/4, bmp.getHeight()/4, true); + + if (root.exists() == false) { + Toast.makeText(mContext, "Side9 folder not found on sdcard", Toast.LENGTH_LONG).show(); + return; + } + + 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) { + + 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; + } + + /* load captions */ + String infoFileName = f.getPath().replace(".jpg", ".txt"); + + File infoFile = new File(infoFileName); + if (infoFile.exists() ) { + entry.caption = cleanString( getFileContent(infoFile) ); + } else { + entry.caption = ""; + } - mBitmapPaths.add( f.getPath() ); - mBitmaps.add(scaled); + mImages.add(entry); } + + Collections.sort(mImages, new ImageEntry.PathComparator() ); + + thumbSize = getSize(); + } + + public void orderByPath() { + Collections.sort(mImages, new ImageEntry.PathComparator()); + this.notifyDataSetChanged(); } + + public void orderByCaption() { + Collections.sort(mImages, new ImageEntry.CaptionComparator()); + this.notifyDataSetChanged(); + } + public int getCount() { - return mBitmaps.size(); + return mImages.size(); } public Object getItem(int position) { @@ -48,15 +130,30 @@ return position; } - public String getImagePath(int position) { - return mBitmapPaths.get(position); + public ImageEntry getImageEntry(int position) { + return mImages.get(position); + } + + public ArrayList getImages() { + return mImages; + } + + private int getSize() { + DisplayMetrics metrics = new DisplayMetrics(); + WindowManager wmgr = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); + wmgr.getDefaultDisplay().getMetrics(metrics); + if (metrics.widthPixels < 480) { + return 150; + } else { + return 200; + } } public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { // if it's not recycled, initialize some attributes imageView = new ImageView(mContext); - imageView.setLayoutParams(new GridView.LayoutParams(200, 200)); + imageView.setLayoutParams(new GridView.LayoutParams(thumbSize, thumbSize)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setAdjustViewBounds(true); imageView.setPadding(4, 4, 4, 4); @@ -64,9 +161,57 @@ imageView = (ImageView) convertView; } - - imageView.setImageBitmap( mBitmaps.get(position) ); + //imageView.setImageURI( Uri.parse(mThumbPaths.get(position)) ); + imageView.setImageBitmap( mImages.get(position).thumb ); return imageView; + } + + String getFileContent(File filename) { + try { + RandomAccessFile raf = new RandomAccessFile(filename, "r"); + int size = (int)raf.length(); + byte buf[] = new byte[size]; + + raf.readFully(buf); + raf.close(); + + return new String(buf); + } catch (IOException e) { + Log.e("Side9", "Error", e); + return ""; + } + } + + String cleanString(String input) { //trim and remove double spaces + String res = input.trim(); + while ( res.contains( " ")) { + res = res.replace(" ", " "); + } + + return res; + } + + + + + class ExtensionFilter implements FilenameFilter { + private String mExt; + public ExtensionFilter(String ext) { + mExt = ext; + } + + @Override + public boolean accept(File dir, String filename) { + String parts[] = filename.split("\\."); + if (parts.length > 1) { + String ext = parts[ parts.length -1]; + if (ext.equals(mExt)) { + return true; + } + } + return false; + } + } } \ No newline at end of file