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

  ViewVC Help
Powered by ViewVC 1.1.20