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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1222 - (hide annotations) (download)
Tue Feb 8 07:32:07 2011 UTC (13 years, 3 months ago) by torben
File size: 6172 byte(s)
Generate the thumb image from widgetprovider after the image has been saved on sdcard, which should lighten the burden on the ImageAdapter when the viewer launches
1 torben 790 package dk.thoerup.side9;
2    
3     import java.io.File;
4 torben 1013 import java.io.FileOutputStream;
5 torben 925 import java.io.FilenameFilter;
6 torben 1013 import java.io.IOException;
7 torben 1185 import java.io.RandomAccessFile;
8 torben 790 import java.util.ArrayList;
9 torben 1185 import java.util.Collection;
10     import java.util.Collections;
11 torben 790
12     import android.content.Context;
13     import android.graphics.Bitmap;
14     import android.graphics.BitmapFactory;
15 torben 1013 import android.graphics.Bitmap.CompressFormat;
16 torben 790 import android.os.Environment;
17 torben 915 import android.util.DisplayMetrics;
18 torben 1013 import android.util.Log;
19 torben 790 import android.view.View;
20     import android.view.ViewGroup;
21 torben 915 import android.view.WindowManager;
22 torben 790 import android.widget.BaseAdapter;
23     import android.widget.GridView;
24     import android.widget.ImageView;
25 torben 912 import android.widget.Toast;
26 torben 790
27     public class ImageAdapter extends BaseAdapter {
28 torben 1222 final static String DIR_NAME = "/Side9";
29     final static String THUMBDIR_NAME = "/.thumb/";
30 torben 1185
31    
32 torben 790 //int mGalleryItemBackground;
33     private Context mContext;
34    
35 torben 1185 ArrayList<ImageEntry> mImages = new ArrayList<ImageEntry>();
36 torben 915
37 torben 1185
38    
39    
40 torben 1212
41 torben 1185 int thumbSize;
42 torben 1222
43     public static Bitmap generateAndLoadThumb(File image) {
44    
45     File thumbDir = new File(Environment.getExternalStorageDirectory().getPath() + DIR_NAME + THUMBDIR_NAME);
46     String fileName = image.getName();
47     File thumb = new File ( thumbDir.getPath() + "/" + fileName );
48    
49     Bitmap thumbBmp = null;
50    
51     if (! thumbDir.exists()) {
52     thumbDir.mkdirs();
53     }
54    
55     if (! thumb.exists() || thumb.lastModified() < image.lastModified()) {
56     Bitmap bmp = BitmapFactory.decodeFile( image.getPath() );
57     thumbBmp = Bitmap.createScaledBitmap(bmp, bmp.getWidth()/4, bmp.getHeight()/4, true);
58 torben 790
59 torben 1222 try {
60     FileOutputStream fos = new FileOutputStream(thumb);
61     thumbBmp.compress(CompressFormat.JPEG, 90, fos);
62     fos.close();
63    
64     } catch (IOException e) {
65     Log.e("Side9", "error", e);
66     }
67    
68     } else {
69     thumbBmp = BitmapFactory.decodeFile( thumb.getPath());
70     }
71     return thumbBmp;
72     }
73    
74 torben 790 public ImageAdapter(Context c) {
75     mContext = c;
76    
77 torben 1222 String path = Environment.getExternalStorageDirectory().getPath() + DIR_NAME ;
78    
79 torben 790
80     File root = new File(path);
81 torben 912
82     if (root.exists() == false) {
83     Toast.makeText(mContext, "Side9 folder not found on sdcard", Toast.LENGTH_LONG).show();
84     return;
85     }
86 torben 925
87     File files[] = root.listFiles( new ExtensionFilter("jpg") );
88 torben 1222
89 torben 912
90 torben 1222 for (File f : files) {
91 torben 1185 ImageEntry entry = new ImageEntry();
92     entry.path = f.getPath();
93 torben 1013
94 torben 1222 entry.thumb = generateAndLoadThumb(f);
95    
96 torben 1013
97 torben 1185 /* load captions */
98     String infoFileName = f.getPath().replace(".jpg", ".txt");
99    
100     File infoFile = new File(infoFileName);
101     if (infoFile.exists() ) {
102     entry.caption = cleanString( getFileContent(infoFile) );
103     } else {
104     entry.caption = "";
105     }
106    
107     mImages.add(entry);
108 torben 1013 }
109    
110 torben 1185 Collections.sort(mImages, new ImageEntry.PathComparator() );
111    
112     thumbSize = getSize();
113 torben 790 }
114 torben 1185
115 torben 1212 public void cleanUp() {
116     mImages.clear();
117     }
118    
119 torben 1185 public void orderByPath() {
120     Collections.sort(mImages, new ImageEntry.PathComparator());
121     this.notifyDataSetChanged();
122     }
123    
124     public void orderByCaption() {
125     Collections.sort(mImages, new ImageEntry.CaptionComparator());
126     this.notifyDataSetChanged();
127     }
128    
129 torben 790
130     public int getCount() {
131 torben 1185 return mImages.size();
132 torben 790 }
133    
134     public Object getItem(int position) {
135     return position;
136     }
137    
138     public long getItemId(int position) {
139     return position;
140     }
141    
142 torben 1185 public ImageEntry getImageEntry(int position) {
143     return mImages.get(position);
144 torben 790 }
145 torben 915
146 torben 1185 public ArrayList<ImageEntry> getImages() {
147     return mImages;
148 torben 925 }
149    
150 torben 915 private int getSize() {
151     DisplayMetrics metrics = new DisplayMetrics();
152     WindowManager wmgr = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
153     wmgr.getDefaultDisplay().getMetrics(metrics);
154     if (metrics.widthPixels < 480) {
155     return 150;
156     } else {
157     return 200;
158     }
159     }
160 torben 790
161     public View getView(int position, View convertView, ViewGroup parent) {
162     ImageView imageView;
163     if (convertView == null) { // if it's not recycled, initialize some attributes
164     imageView = new ImageView(mContext);
165 torben 1185 imageView.setLayoutParams(new GridView.LayoutParams(thumbSize, thumbSize));
166 torben 790 imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
167     imageView.setAdjustViewBounds(true);
168     imageView.setPadding(4, 4, 4, 4);
169     } else {
170     imageView = (ImageView) convertView;
171     }
172    
173 torben 1013 //imageView.setImageURI( Uri.parse(mThumbPaths.get(position)) );
174 torben 1185 imageView.setImageBitmap( mImages.get(position).thumb );
175 torben 790 return imageView;
176 torben 925 }
177    
178 torben 1185 String getFileContent(File filename) {
179     try {
180     RandomAccessFile raf = new RandomAccessFile(filename, "r");
181     int size = (int)raf.length();
182     byte buf[] = new byte[size];
183    
184 torben 1186 raf.readFully(buf);
185     raf.close();
186    
187 torben 1185 return new String(buf);
188     } catch (IOException e) {
189     Log.e("Side9", "Error", e);
190     return "";
191     }
192     }
193    
194     String cleanString(String input) { //trim and remove double spaces
195     String res = input.trim();
196     while ( res.contains( " ")) {
197     res = res.replace(" ", " ");
198     }
199    
200     return res;
201     }
202    
203    
204    
205    
206    
207 torben 925 class ExtensionFilter implements FilenameFilter {
208     private String mExt;
209     public ExtensionFilter(String ext) {
210     mExt = ext;
211     }
212 torben 790
213 torben 925 @Override
214     public boolean accept(File dir, String filename) {
215     String parts[] = filename.split("\\.");
216     if (parts.length > 1) {
217     String ext = parts[ parts.length -1];
218     if (ext.equals(mExt)) {
219     return true;
220     }
221     }
222     return false;
223     }
224    
225 torben 790 }
226     }

  ViewVC Help
Powered by ViewVC 1.1.20