/[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 1222 by torben, Tue Feb 8 07:32:07 2011 UTC revision 1223 by torben, Wed Feb 9 07:14:53 2011 UTC
# Line 28  public class ImageAdapter extends BaseAd Line 28  public class ImageAdapter extends BaseAd
28          final static String DIR_NAME = "/Side9";          final static String DIR_NAME = "/Side9";
29          final static String THUMBDIR_NAME = "/.thumb/";          final static String THUMBDIR_NAME = "/.thumb/";
30    
31                    //int mGalleryItemBackground;
     //int mGalleryItemBackground;  
32      private Context mContext;      private Context mContext;
33            
34      ArrayList<ImageEntry> mImages = new ArrayList<ImageEntry>();      ArrayList<ImageEntry> mImages = new ArrayList<ImageEntry>();
35            
       
       
       
       
36      int thumbSize;      int thumbSize;
37            
     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);  
38    
                 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;  
     }  
39    
40      public ImageAdapter(Context c) {      public ImageAdapter(Context c) {
41          mContext = c;          mContext = c;
# Line 91  public class ImageAdapter extends BaseAd Line 57  public class ImageAdapter extends BaseAd
57                  ImageEntry entry = new ImageEntry();                  ImageEntry entry = new ImageEntry();
58                  entry.path = f.getPath();                  entry.path = f.getPath();
59    
60                  entry.thumb = generateAndLoadThumb(f);                  //entry.thumb = generateAndLoadThumb(f);
61    
62                                    
63                  /* load captions */                  /* load captions */
# Line 116  public class ImageAdapter extends BaseAd Line 82  public class ImageAdapter extends BaseAd
82          mImages.clear();          mImages.clear();
83      }      }
84            
85        public static Bitmap generateAndLoadThumb(File image) {
86            
87            File thumbDir = new File(Environment.getExternalStorageDirectory().getPath() + DIR_NAME + THUMBDIR_NAME);
88            String fileName = image.getName();
89            File thumb = new File ( thumbDir.getPath() + "/" + fileName );
90            
91            Bitmap thumbBmp = null;
92            
93            if (! thumbDir.exists()) {
94                    thumbDir.mkdirs();
95            }
96            
97            if (! thumb.exists() || thumb.lastModified() < image.lastModified()) {
98                    Bitmap bmp = BitmapFactory.decodeFile( image.getPath() );
99                    thumbBmp =  Bitmap.createScaledBitmap(bmp, bmp.getWidth()/4, bmp.getHeight()/4, true);
100    
101                    try {
102                            FileOutputStream fos = new FileOutputStream(thumb);
103                            thumbBmp.compress(CompressFormat.JPEG, 90, fos);  
104                            fos.close();
105                            
106                    } catch (IOException e) {
107                            Log.e("Side9", "error", e);
108                    }
109                    
110            } else {
111                    thumbBmp = BitmapFactory.decodeFile( thumb.getPath());
112            }      
113            return thumbBmp;
114        }
115        
116      public void orderByPath() {      public void orderByPath() {
117          Collections.sort(mImages, new ImageEntry.PathComparator());          Collections.sort(mImages, new ImageEntry.PathComparator());
118          this.notifyDataSetChanged();          this.notifyDataSetChanged();
# Line 159  public class ImageAdapter extends BaseAd Line 156  public class ImageAdapter extends BaseAd
156      }      }
157    
158      public View getView(int position, View convertView, ViewGroup parent) {      public View getView(int position, View convertView, ViewGroup parent) {
159            
160            if (position >= mImages.size()) {
161                    return null;
162            }
163            
164            ImageEntry currentEntry = mImages.get(position);
165    
166            if (currentEntry.thumb == null ) {
167                    File thumb = new File ( currentEntry.path );
168                    currentEntry.thumb = generateAndLoadThumb(thumb);
169            }
170            
171          ImageView imageView;          ImageView imageView;
172          if (convertView == null) {  // if it's not recycled, initialize some attributes          if (convertView == null) {  // if it's not recycled, initialize some attributes
173              imageView = new ImageView(mContext);              imageView = new ImageView(mContext);
# Line 171  public class ImageAdapter extends BaseAd Line 180  public class ImageAdapter extends BaseAd
180          }          }
181    
182          //imageView.setImageURI(  Uri.parse(mThumbPaths.get(position)) );          //imageView.setImageURI(  Uri.parse(mThumbPaths.get(position)) );
183          imageView.setImageBitmap( mImages.get(position).thumb );          imageView.setImageBitmap( currentEntry.thumb );
184          return imageView;          return imageView;
185      }      }
186            

Legend:
Removed from v.1222  
changed lines
  Added in v.1223

  ViewVC Help
Powered by ViewVC 1.1.20