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