/[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 1185 by torben, Tue Nov 2 17:08:57 2010 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        int thumbSize;
40    
41      public ImageAdapter(Context c) {      public ImageAdapter(Context c) {
42          mContext = c;          mContext = c;
# Line 42  public class ImageAdapter extends BaseAd Line 54  public class ImageAdapter extends BaseAd
54    
55          File files[] = root.listFiles( new ExtensionFilter("jpg") );          File files[] = root.listFiles( new ExtensionFilter("jpg") );
56                    
57          for (File f : files) {            
58              Bitmap bmp = BitmapFactory.decodeFile( f.getPath() );          String thumbPath =  path + "/.thumb/";
59              Bitmap scaled =  Bitmap.createScaledBitmap(bmp, bmp.getWidth()/4, bmp.getHeight()/4, true);          
60            File thumbDir = new File(thumbPath);
61            if (! thumbDir.exists()) {
62                    thumbDir.mkdirs();
63            }
64            
65            
66            for (File f : files) {
67                    
68                    ImageEntry entry = new ImageEntry();
69                    entry.path = f.getPath();
70    
71                    String fileName = f.getName();
72                    File thumb = new File ( thumbPath + fileName );
73                    
74                    if (! thumb.exists() || thumb.lastModified() < f.lastModified()) {
75                            Bitmap bmp = BitmapFactory.decodeFile( f.getPath() );
76                            Bitmap scaled =  Bitmap.createScaledBitmap(bmp, bmp.getWidth()/4, bmp.getHeight()/4, true);
77                            entry.thumb = scaled;
78                            try {
79                                    FileOutputStream fos = new FileOutputStream(thumb);
80                                    scaled.compress(CompressFormat.JPEG, 90, fos);                          
81                                    
82                            } catch (IOException e) {
83                                    Log.e("Side9", "error", e);
84                            }
85                            
86                    } else {
87                            Bitmap thumbBmp = BitmapFactory.decodeFile( thumb.getPath());
88                            entry.thumb = thumbBmp;
89                    }
90                    
91                    /* load captions */
92                    String infoFileName = f.getPath().replace(".jpg", ".txt");
93    
94                    File infoFile = new File(infoFileName);
95                    if (infoFile.exists() ) {
96                            entry.caption = cleanString( getFileContent(infoFile) );
97                    } else {
98                            entry.caption = "";
99                    }              
100                            
101                  mBitmapPaths.add(  f.getPath() );                  mImages.add(entry);
                 mBitmaps.add(scaled);  
102          }          }
103                    
104          size = getSize();          Collections.sort(mImages,  new ImageEntry.PathComparator() );
105            
106            thumbSize = getSize();
107        }
108        
109        public void orderByPath() {
110            Collections.sort(mImages, new ImageEntry.PathComparator());
111            this.notifyDataSetChanged();
112        }
113        
114        public void orderByCaption() {
115            Collections.sort(mImages, new ImageEntry.CaptionComparator());
116            this.notifyDataSetChanged();
117      }      }
118        
119    
120      public int getCount() {      public int getCount() {
121          return mBitmaps.size();          return mImages.size();
122      }      }
123    
124      public Object getItem(int position) {      public Object getItem(int position) {
# Line 65  public class ImageAdapter extends BaseAd Line 129  public class ImageAdapter extends BaseAd
129          return position;          return position;
130      }      }
131            
132      public String getImagePath(int position) {      public ImageEntry getImageEntry(int position) {
133          return mBitmapPaths.get(position);          return mImages.get(position);
134      }      }
135            
136      public ArrayList<String> getImagePaths() {      public ArrayList<ImageEntry> getImages() {
137          return mBitmapPaths;          return mImages;
138      }      }
139            
140      private int getSize() {      private int getSize() {
# Line 88  public class ImageAdapter extends BaseAd Line 152  public class ImageAdapter extends BaseAd
152          ImageView imageView;          ImageView imageView;
153          if (convertView == null) {  // if it's not recycled, initialize some attributes          if (convertView == null) {  // if it's not recycled, initialize some attributes
154              imageView = new ImageView(mContext);              imageView = new ImageView(mContext);
155              imageView.setLayoutParams(new GridView.LayoutParams(size, size));              imageView.setLayoutParams(new GridView.LayoutParams(thumbSize, thumbSize));
156              imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);              imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
157              imageView.setAdjustViewBounds(true);                imageView.setAdjustViewBounds(true);  
158              imageView.setPadding(4, 4, 4, 4);              imageView.setPadding(4, 4, 4, 4);
# Line 96  public class ImageAdapter extends BaseAd Line 160  public class ImageAdapter extends BaseAd
160              imageView = (ImageView) convertView;              imageView = (ImageView) convertView;
161          }          }
162    
163            //imageView.setImageURI(  Uri.parse(mThumbPaths.get(position)) );
164          imageView.setImageBitmap( mBitmaps.get(position) );          imageView.setImageBitmap( mImages.get(position).thumb );
165          return imageView;          return imageView;
166      }      }
167            
168            String getFileContent(File filename) {
169                    try {
170                            RandomAccessFile raf = new RandomAccessFile(filename, "r");
171                            int size = (int)raf.length();
172                            byte buf[] = new byte[size];
173                            
174                            raf.readFully(buf);
175                            return new String(buf);                
176                    } catch (IOException e) {
177                            Log.e("Side9", "Error", e);
178                            return "";
179                    }
180            }
181            
182            String cleanString(String input) { //trim and remove double spaces
183                    String res = input.trim();
184                    while ( res.contains( "  ")) {
185                            res = res.replace("  ", " ");
186                    }
187                    
188                    return res;
189            }
190            
191    
192            
193            
194        
195      class ExtensionFilter implements FilenameFilter {      class ExtensionFilter implements FilenameFilter {
196          private String mExt;          private String mExt;
197          public ExtensionFilter(String ext) {          public ExtensionFilter(String ext) {

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

  ViewVC Help
Powered by ViewVC 1.1.20