/[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 1224 - (show annotations) (download)
Wed Feb 9 17:39:54 2011 UTC (13 years, 3 months ago) by torben
File size: 6477 byte(s)
Cleanup and annotations
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
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 @Override
127 public int getCount() {
128 return mImages.size();
129 }
130
131 @Override
132 public Object getItem(int position) {
133 return position;
134 }
135
136 @Override
137 public long getItemId(int position) {
138 return position;
139 }
140
141 public ImageEntry getImageEntry(int position) {
142 return mImages.get(position);
143 }
144
145 public ArrayList<ImageEntry> getImages() {
146 return mImages;
147 }
148
149 private int getSize() {
150 DisplayMetrics metrics = new DisplayMetrics();
151 WindowManager wmgr = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
152 wmgr.getDefaultDisplay().getMetrics(metrics);
153 if (metrics.widthPixels < 480) {
154 return 150;
155 } else {
156 return 200;
157 }
158 }
159
160 @Override
161 public View getView(int position, View convertView, ViewGroup parent) {
162
163 if (position >= mImages.size()) {
164 return null;
165 }
166
167 ImageEntry currentEntry = mImages.get(position);
168
169 if (currentEntry.thumb == null ) {
170 File thumb = new File ( currentEntry.path );
171 currentEntry.thumb = generateAndLoadThumb(thumb);
172 }
173
174 ImageView imageView;
175 if (convertView == null) { // if it's not recycled, initialize some attributes
176 imageView = new ImageView(mContext);
177 imageView.setLayoutParams(new GridView.LayoutParams(thumbSize, thumbSize));
178 imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
179 imageView.setAdjustViewBounds(true);
180 imageView.setPadding(4, 4, 4, 4);
181 } else {
182 imageView = (ImageView) convertView;
183 }
184
185 //imageView.setImageURI( Uri.parse(mThumbPaths.get(position)) );
186 imageView.setImageBitmap( currentEntry.thumb );
187 return imageView;
188 }
189
190 String getFileContent(File filename) {
191 try {
192 RandomAccessFile raf = new RandomAccessFile(filename, "r");
193 int size = (int)raf.length();
194 byte buf[] = new byte[size];
195
196 raf.readFully(buf);
197 raf.close();
198
199 return new String(buf);
200 } catch (IOException e) {
201 Log.e("Side9", "Error", e);
202 return "";
203 }
204 }
205
206 String cleanString(String input) { //trim and remove double spaces
207 String res = input.trim();
208 while ( res.contains( " ")) {
209 res = res.replace(" ", " ");
210 }
211
212 return res;
213 }
214
215
216
217
218
219 class ExtensionFilter implements FilenameFilter {
220 private String mExt;
221 public ExtensionFilter(String ext) {
222 mExt = ext;
223 }
224
225 @Override
226 public boolean accept(File dir, String filename) {
227 String parts[] = filename.split("\\.");
228 if (parts.length > 1) {
229 String ext = parts[ parts.length -1];
230 if (ext.equals(mExt)) {
231 return true;
232 }
233 }
234 return false;
235 }
236
237 }
238 }

  ViewVC Help
Powered by ViewVC 1.1.20