/[projects]/android/Side9/src/dk/thoerup/side9/ImageAdapter.java
ViewVC logotype

Diff of /android/Side9/src/dk/thoerup/side9/ImageAdapter.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 925 by torben, Sun Jun 27 10:41:10 2010 UTC revision 1212 by torben, Fri Jan 7 07:15:42 2011 UTC
# Line 1  Line 1 
1  package dk.thoerup.side9;  package dk.thoerup.side9;
2    
3  import java.io.File;  import java.io.File;
4    import java.io.FileOutputStream;
5  import java.io.FilenameFilter;  import java.io.FilenameFilter;
6    import java.io.IOException;
7    import java.io.RandomAccessFile;
8  import java.util.ArrayList;  import java.util.ArrayList;
9    import java.util.Collection;
10    import java.util.Collections;
11    
12  import android.content.Context;  import android.content.Context;
13  import android.graphics.Bitmap;  import android.graphics.Bitmap;
14  import android.graphics.BitmapFactory;  import android.graphics.BitmapFactory;
15    import android.graphics.Bitmap.CompressFormat;
16  import android.os.Environment;  import android.os.Environment;
17  import android.util.DisplayMetrics;  import android.util.DisplayMetrics;
18    import android.util.Log;
19  import android.view.View;  import android.view.View;
20  import android.view.ViewGroup;  import android.view.ViewGroup;
21  import android.view.WindowManager;  import android.view.WindowManager;
# Line 18  import android.widget.ImageView; Line 25  import android.widget.ImageView;
25  import android.widget.Toast;  import android.widget.Toast;
26    
27  public class ImageAdapter extends BaseAdapter {  public class ImageAdapter extends BaseAdapter {
28            
29    
30            
31      //int mGalleryItemBackground;      //int mGalleryItemBackground;
32      private Context mContext;      private Context mContext;
33            
34      ArrayList<String> mBitmapPaths = new ArrayList<String>();      ArrayList<ImageEntry> mImages = new ArrayList<ImageEntry>();
35      ArrayList<Bitmap> mBitmaps = new ArrayList<Bitmap>();      
36        
37            
38      int size;      
39        
40        int thumbSize;
41    
42      public ImageAdapter(Context c) {      public ImageAdapter(Context c) {
43          mContext = c;          mContext = c;
# Line 42  public class ImageAdapter extends BaseAd Line 55  public class ImageAdapter extends BaseAd
55    
56          File files[] = root.listFiles( new ExtensionFilter("jpg") );          File files[] = root.listFiles( new ExtensionFilter("jpg") );
57                    
58          for (File f : files) {            
59              Bitmap bmp = BitmapFactory.decodeFile( f.getPath() );          String thumbPath =  path + "/.thumb/";
60              Bitmap scaled =  Bitmap.createScaledBitmap(bmp, bmp.getWidth()/4, bmp.getHeight()/4, true);          
61            File thumbDir = new File(thumbPath);
62            if (! thumbDir.exists()) {
63                    thumbDir.mkdirs();
64            }
65            
66            
67            for (File f : files) {
68                    
69                    ImageEntry entry = new ImageEntry();
70                    entry.path = f.getPath();
71    
72                    String fileName = f.getName();
73                    File thumb = new File ( thumbPath + fileName );
74                    
75                    if (! thumb.exists() || thumb.lastModified() < f.lastModified()) {
76                            Bitmap bmp = BitmapFactory.decodeFile( f.getPath() );
77                            Bitmap scaled =  Bitmap.createScaledBitmap(bmp, bmp.getWidth()/4, bmp.getHeight()/4, true);
78                            entry.thumb = scaled;
79                            try {
80                                    FileOutputStream fos = new FileOutputStream(thumb);
81                                    scaled.compress(CompressFormat.JPEG, 90, fos);  
82                                    fos.close();
83                                    
84                            } catch (IOException e) {
85                                    Log.e("Side9", "error", e);
86                            }
87                            
88                    } else {
89                            Bitmap thumbBmp = BitmapFactory.decodeFile( thumb.getPath());
90                            entry.thumb = thumbBmp;
91                    }
92                    
93                    /* load captions */
94                    String infoFileName = f.getPath().replace(".jpg", ".txt");
95    
96                    File infoFile = new File(infoFileName);
97                    if (infoFile.exists() ) {
98                            entry.caption = cleanString( getFileContent(infoFile) );
99                    } else {
100                            entry.caption = "";
101                    }              
102                            
103                  mBitmapPaths.add(  f.getPath() );                  mImages.add(entry);
                 mBitmaps.add(scaled);  
104          }          }
105                    
106          size = getSize();          Collections.sort(mImages,  new ImageEntry.PathComparator() );
107            
108            thumbSize = getSize();
109      }      }
110        
111        public void cleanUp() {
112            mImages.clear();
113        }
114        
115        public void orderByPath() {
116            Collections.sort(mImages, new ImageEntry.PathComparator());
117            this.notifyDataSetChanged();
118        }
119        
120        public void orderByCaption() {
121            Collections.sort(mImages, new ImageEntry.CaptionComparator());
122            this.notifyDataSetChanged();
123        }
124        
125    
126      public int getCount() {      public int getCount() {
127          return mBitmaps.size();          return mImages.size();
128      }      }
129    
130      public Object getItem(int position) {      public Object getItem(int position) {
# Line 65  public class ImageAdapter extends BaseAd Line 135  public class ImageAdapter extends BaseAd
135          return position;          return position;
136      }      }
137            
138      public String getImagePath(int position) {      public ImageEntry getImageEntry(int position) {
139          return mBitmapPaths.get(position);          return mImages.get(position);
140      }      }
141            
142      public ArrayList<String> getImagePaths() {      public ArrayList<ImageEntry> getImages() {
143          return mBitmapPaths;          return mImages;
144      }      }
145            
146      private int getSize() {      private int getSize() {
# Line 88  public class ImageAdapter extends BaseAd Line 158  public class ImageAdapter extends BaseAd
158          ImageView imageView;          ImageView imageView;
159          if (convertView == null) {  // if it's not recycled, initialize some attributes          if (convertView == null) {  // if it's not recycled, initialize some attributes
160              imageView = new ImageView(mContext);              imageView = new ImageView(mContext);
161              imageView.setLayoutParams(new GridView.LayoutParams(size, size));              imageView.setLayoutParams(new GridView.LayoutParams(thumbSize, thumbSize));
162              imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);              imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
163              imageView.setAdjustViewBounds(true);                imageView.setAdjustViewBounds(true);  
164              imageView.setPadding(4, 4, 4, 4);              imageView.setPadding(4, 4, 4, 4);
# Line 96  public class ImageAdapter extends BaseAd Line 166  public class ImageAdapter extends BaseAd
166              imageView = (ImageView) convertView;              imageView = (ImageView) convertView;
167          }          }
168    
169            //imageView.setImageURI(  Uri.parse(mThumbPaths.get(position)) );
170          imageView.setImageBitmap( mBitmaps.get(position) );          imageView.setImageBitmap( mImages.get(position).thumb );
171          return imageView;          return imageView;
172      }      }
173            
174            String getFileContent(File filename) {
175                    try {
176                            RandomAccessFile raf = new RandomAccessFile(filename, "r");
177                            int size = (int)raf.length();
178                            byte buf[] = new byte[size];
179                            
180                            raf.readFully(buf);                    
181                            raf.close();
182                            
183                            return new String(buf);                
184                    } catch (IOException e) {
185                            Log.e("Side9", "Error", e);
186                            return "";
187                    }
188            }
189            
190            String cleanString(String input) { //trim and remove double spaces
191                    String res = input.trim();
192                    while ( res.contains( "  ")) {
193                            res = res.replace("  ", " ");
194                    }
195                    
196                    return res;
197            }
198            
199    
200            
201            
202        
203      class ExtensionFilter implements FilenameFilter {      class ExtensionFilter implements FilenameFilter {
204          private String mExt;          private String mExt;
205          public ExtensionFilter(String ext) {          public ExtensionFilter(String ext) {

Legend:
Removed from v.925  
changed lines
  Added in v.1212

  ViewVC Help
Powered by ViewVC 1.1.20