--- android/Side9/src/dk/thoerup/side9/ImageAdapter.java 2010/11/02 13:53:49 1184 +++ android/Side9/src/dk/thoerup/side9/ImageAdapter.java 2010/11/02 17:08:57 1185 @@ -4,13 +4,15 @@ 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.net.Uri; import android.os.Environment; import android.util.DisplayMetrics; import android.util.Log; @@ -23,13 +25,18 @@ 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 size; + int thumbSize; public ImageAdapter(Context c) { mContext = c; @@ -57,6 +64,9 @@ for (File f : files) { + + ImageEntry entry = new ImageEntry(); + entry.path = f.getPath(); String fileName = f.getName(); File thumb = new File ( thumbPath + fileName ); @@ -64,7 +74,7 @@ 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); - mBitmaps.add(scaled); + entry.thumb = scaled; try { FileOutputStream fos = new FileOutputStream(thumb); scaled.compress(CompressFormat.JPEG, 90, fos); @@ -75,17 +85,40 @@ } else { Bitmap thumbBmp = BitmapFactory.decodeFile( thumb.getPath()); - mBitmaps.add(thumbBmp); + entry.thumb = thumbBmp; } - mBitmapPaths.add( f.getPath() ); + /* 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 = ""; + } + + mImages.add(entry); } - size = getSize(); + 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 mBitmapPaths.size(); + return mImages.size(); } public Object getItem(int position) { @@ -96,12 +129,12 @@ return position; } - public String getImagePath(int position) { - return mBitmapPaths.get(position); + public ImageEntry getImageEntry(int position) { + return mImages.get(position); } - public ArrayList getImagePaths() { - return mBitmapPaths; + public ArrayList getImages() { + return mImages; } private int getSize() { @@ -119,7 +152,7 @@ ImageView imageView; if (convertView == null) { // if it's not recycled, initialize some attributes imageView = new ImageView(mContext); - imageView.setLayoutParams(new GridView.LayoutParams(size, size)); + imageView.setLayoutParams(new GridView.LayoutParams(thumbSize, thumbSize)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setAdjustViewBounds(true); imageView.setPadding(4, 4, 4, 4); @@ -128,10 +161,37 @@ } //imageView.setImageURI( Uri.parse(mThumbPaths.get(position)) ); - imageView.setImageBitmap( mBitmaps.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); + 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) {