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

  ViewVC Help
Powered by ViewVC 1.1.20