/[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 1186 - (show annotations) (download)
Wed Nov 3 05:21:48 2010 UTC (13 years, 6 months ago) by torben
File size: 5932 byte(s)
Make sure the IO streams are closed

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

  ViewVC Help
Powered by ViewVC 1.1.20