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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1222 - (show 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 package dk.thoerup.side9;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.FilenameFilter;
6 import java.io.IOException;
7 import java.io.RandomAccessFile;
8 import java.util.ArrayList;
9 import java.util.Collection;
10 import java.util.Collections;
11
12 import android.content.Context;
13 import android.graphics.Bitmap;
14 import android.graphics.BitmapFactory;
15 import android.graphics.Bitmap.CompressFormat;
16 import android.os.Environment;
17 import android.util.DisplayMetrics;
18 import android.util.Log;
19 import android.view.View;
20 import android.view.ViewGroup;
21 import android.view.WindowManager;
22 import android.widget.BaseAdapter;
23 import android.widget.GridView;
24 import android.widget.ImageView;
25 import android.widget.Toast;
26
27 public class ImageAdapter extends BaseAdapter {
28 final static String DIR_NAME = "/Side9";
29 final static String THUMBDIR_NAME = "/.thumb/";
30
31
32 //int mGalleryItemBackground;
33 private Context mContext;
34
35 ArrayList<ImageEntry> mImages = new ArrayList<ImageEntry>();
36
37
38
39
40
41 int thumbSize;
42
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
59 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 public ImageAdapter(Context c) {
75 mContext = c;
76
77 String path = Environment.getExternalStorageDirectory().getPath() + DIR_NAME ;
78
79
80 File root = new File(path);
81
82 if (root.exists() == false) {
83 Toast.makeText(mContext, "Side9 folder not found on sdcard", Toast.LENGTH_LONG).show();
84 return;
85 }
86
87 File files[] = root.listFiles( new ExtensionFilter("jpg") );
88
89
90 for (File f : files) {
91 ImageEntry entry = new ImageEntry();
92 entry.path = f.getPath();
93
94 entry.thumb = generateAndLoadThumb(f);
95
96
97 /* 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 }
109
110 Collections.sort(mImages, new ImageEntry.PathComparator() );
111
112 thumbSize = getSize();
113 }
114
115 public void cleanUp() {
116 mImages.clear();
117 }
118
119 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
130 public int getCount() {
131 return mImages.size();
132 }
133
134 public Object getItem(int position) {
135 return position;
136 }
137
138 public long getItemId(int position) {
139 return position;
140 }
141
142 public ImageEntry getImageEntry(int position) {
143 return mImages.get(position);
144 }
145
146 public ArrayList<ImageEntry> getImages() {
147 return mImages;
148 }
149
150 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
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 imageView.setLayoutParams(new GridView.LayoutParams(thumbSize, thumbSize));
166 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 //imageView.setImageURI( Uri.parse(mThumbPaths.get(position)) );
174 imageView.setImageBitmap( mImages.get(position).thumb );
175 return imageView;
176 }
177
178 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 raf.readFully(buf);
185 raf.close();
186
187 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 class ExtensionFilter implements FilenameFilter {
208 private String mExt;
209 public ExtensionFilter(String ext) {
210 mExt = ext;
211 }
212
213 @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 }
226 }

  ViewVC Help
Powered by ViewVC 1.1.20