/[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 1223 - (show annotations) (download)
Wed Feb 9 07:14:53 2011 UTC (13 years, 3 months ago) by torben
File size: 6447 byte(s)
(Experimental) load the thumbs ad-hoc but cache the thumb once it's loaded

Bump version to 23
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 //int mGalleryItemBackground;
32 private Context mContext;
33
34 ArrayList<ImageEntry> mImages = new ArrayList<ImageEntry>();
35
36 int thumbSize;
37
38
39
40 public ImageAdapter(Context c) {
41 mContext = c;
42
43 String path = Environment.getExternalStorageDirectory().getPath() + DIR_NAME ;
44
45
46 File root = new File(path);
47
48 if (root.exists() == false) {
49 Toast.makeText(mContext, "Side9 folder not found on sdcard", Toast.LENGTH_LONG).show();
50 return;
51 }
52
53 File files[] = root.listFiles( new ExtensionFilter("jpg") );
54
55
56 for (File f : files) {
57 ImageEntry entry = new ImageEntry();
58 entry.path = f.getPath();
59
60 //entry.thumb = generateAndLoadThumb(f);
61
62
63 /* load captions */
64 String infoFileName = f.getPath().replace(".jpg", ".txt");
65
66 File infoFile = new File(infoFileName);
67 if (infoFile.exists() ) {
68 entry.caption = cleanString( getFileContent(infoFile) );
69 } else {
70 entry.caption = "";
71 }
72
73 mImages.add(entry);
74 }
75
76 Collections.sort(mImages, new ImageEntry.PathComparator() );
77
78 thumbSize = getSize();
79 }
80
81 public void cleanUp() {
82 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() {
117 Collections.sort(mImages, new ImageEntry.PathComparator());
118 this.notifyDataSetChanged();
119 }
120
121 public void orderByCaption() {
122 Collections.sort(mImages, new ImageEntry.CaptionComparator());
123 this.notifyDataSetChanged();
124 }
125
126
127 public int getCount() {
128 return mImages.size();
129 }
130
131 public Object getItem(int position) {
132 return position;
133 }
134
135 public long getItemId(int position) {
136 return position;
137 }
138
139 public ImageEntry getImageEntry(int position) {
140 return mImages.get(position);
141 }
142
143 public ArrayList<ImageEntry> getImages() {
144 return mImages;
145 }
146
147 private int getSize() {
148 DisplayMetrics metrics = new DisplayMetrics();
149 WindowManager wmgr = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
150 wmgr.getDefaultDisplay().getMetrics(metrics);
151 if (metrics.widthPixels < 480) {
152 return 150;
153 } else {
154 return 200;
155 }
156 }
157
158 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;
172 if (convertView == null) { // if it's not recycled, initialize some attributes
173 imageView = new ImageView(mContext);
174 imageView.setLayoutParams(new GridView.LayoutParams(thumbSize, thumbSize));
175 imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
176 imageView.setAdjustViewBounds(true);
177 imageView.setPadding(4, 4, 4, 4);
178 } else {
179 imageView = (ImageView) convertView;
180 }
181
182 //imageView.setImageURI( Uri.parse(mThumbPaths.get(position)) );
183 imageView.setImageBitmap( currentEntry.thumb );
184 return imageView;
185 }
186
187 String getFileContent(File filename) {
188 try {
189 RandomAccessFile raf = new RandomAccessFile(filename, "r");
190 int size = (int)raf.length();
191 byte buf[] = new byte[size];
192
193 raf.readFully(buf);
194 raf.close();
195
196 return new String(buf);
197 } catch (IOException e) {
198 Log.e("Side9", "Error", e);
199 return "";
200 }
201 }
202
203 String cleanString(String input) { //trim and remove double spaces
204 String res = input.trim();
205 while ( res.contains( " ")) {
206 res = res.replace(" ", " ");
207 }
208
209 return res;
210 }
211
212
213
214
215
216 class ExtensionFilter implements FilenameFilter {
217 private String mExt;
218 public ExtensionFilter(String ext) {
219 mExt = ext;
220 }
221
222 @Override
223 public boolean accept(File dir, String filename) {
224 String parts[] = filename.split("\\.");
225 if (parts.length > 1) {
226 String ext = parts[ parts.length -1];
227 if (ext.equals(mExt)) {
228 return true;
229 }
230 }
231 return false;
232 }
233
234 }
235 }

  ViewVC Help
Powered by ViewVC 1.1.20