1 |
torben |
790 |
package dk.thoerup.side9;
|
2 |
|
|
|
3 |
|
|
import java.io.File;
|
4 |
torben |
1013 |
import java.io.FileOutputStream;
|
5 |
torben |
925 |
import java.io.FilenameFilter;
|
6 |
torben |
1013 |
import java.io.IOException;
|
7 |
torben |
1185 |
import java.io.RandomAccessFile;
|
8 |
torben |
790 |
import java.util.ArrayList;
|
9 |
torben |
1185 |
import java.util.Collection;
|
10 |
|
|
import java.util.Collections;
|
11 |
torben |
790 |
|
12 |
|
|
import android.content.Context;
|
13 |
|
|
import android.graphics.Bitmap;
|
14 |
|
|
import android.graphics.BitmapFactory;
|
15 |
torben |
1013 |
import android.graphics.Bitmap.CompressFormat;
|
16 |
torben |
790 |
import android.os.Environment;
|
17 |
torben |
915 |
import android.util.DisplayMetrics;
|
18 |
torben |
1013 |
import android.util.Log;
|
19 |
torben |
790 |
import android.view.View;
|
20 |
|
|
import android.view.ViewGroup;
|
21 |
torben |
915 |
import android.view.WindowManager;
|
22 |
torben |
790 |
import android.widget.BaseAdapter;
|
23 |
|
|
import android.widget.GridView;
|
24 |
|
|
import android.widget.ImageView;
|
25 |
torben |
912 |
import android.widget.Toast;
|
26 |
torben |
790 |
|
27 |
|
|
public class ImageAdapter extends BaseAdapter {
|
28 |
torben |
1222 |
final static String DIR_NAME = "/Side9";
|
29 |
|
|
final static String THUMBDIR_NAME = "/.thumb/";
|
30 |
torben |
1185 |
|
31 |
torben |
1223 |
//int mGalleryItemBackground;
|
32 |
torben |
790 |
private Context mContext;
|
33 |
|
|
|
34 |
torben |
1185 |
ArrayList<ImageEntry> mImages = new ArrayList<ImageEntry>();
|
35 |
torben |
915 |
|
36 |
torben |
1185 |
int thumbSize;
|
37 |
torben |
1222 |
|
38 |
torben |
790 |
|
39 |
torben |
1222 |
|
40 |
torben |
790 |
public ImageAdapter(Context c) {
|
41 |
|
|
mContext = c;
|
42 |
|
|
|
43 |
torben |
1222 |
String path = Environment.getExternalStorageDirectory().getPath() + DIR_NAME ;
|
44 |
|
|
|
45 |
torben |
790 |
|
46 |
|
|
File root = new File(path);
|
47 |
torben |
912 |
|
48 |
|
|
if (root.exists() == false) {
|
49 |
|
|
Toast.makeText(mContext, "Side9 folder not found on sdcard", Toast.LENGTH_LONG).show();
|
50 |
|
|
return;
|
51 |
|
|
}
|
52 |
torben |
925 |
|
53 |
|
|
File files[] = root.listFiles( new ExtensionFilter("jpg") );
|
54 |
torben |
1222 |
|
55 |
torben |
912 |
|
56 |
torben |
1222 |
for (File f : files) {
|
57 |
torben |
1185 |
ImageEntry entry = new ImageEntry();
|
58 |
|
|
entry.path = f.getPath();
|
59 |
torben |
1013 |
|
60 |
torben |
1223 |
//entry.thumb = generateAndLoadThumb(f);
|
61 |
torben |
1222 |
|
62 |
torben |
1013 |
|
63 |
torben |
1185 |
/* 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 |
torben |
1013 |
}
|
75 |
|
|
|
76 |
torben |
1185 |
Collections.sort(mImages, new ImageEntry.PathComparator() );
|
77 |
|
|
|
78 |
|
|
thumbSize = getSize();
|
79 |
torben |
790 |
}
|
80 |
torben |
1185 |
|
81 |
torben |
1212 |
public void cleanUp() {
|
82 |
|
|
mImages.clear();
|
83 |
|
|
}
|
84 |
|
|
|
85 |
torben |
1223 |
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 |
torben |
1185 |
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 |
torben |
790 |
|
127 |
|
|
public int getCount() {
|
128 |
torben |
1185 |
return mImages.size();
|
129 |
torben |
790 |
}
|
130 |
|
|
|
131 |
|
|
public Object getItem(int position) {
|
132 |
|
|
return position;
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
public long getItemId(int position) {
|
136 |
|
|
return position;
|
137 |
|
|
}
|
138 |
|
|
|
139 |
torben |
1185 |
public ImageEntry getImageEntry(int position) {
|
140 |
|
|
return mImages.get(position);
|
141 |
torben |
790 |
}
|
142 |
torben |
915 |
|
143 |
torben |
1185 |
public ArrayList<ImageEntry> getImages() {
|
144 |
|
|
return mImages;
|
145 |
torben |
925 |
}
|
146 |
|
|
|
147 |
torben |
915 |
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 |
torben |
790 |
|
158 |
|
|
public View getView(int position, View convertView, ViewGroup parent) {
|
159 |
torben |
1223 |
|
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 |
torben |
790 |
ImageView imageView;
|
172 |
|
|
if (convertView == null) { // if it's not recycled, initialize some attributes
|
173 |
|
|
imageView = new ImageView(mContext);
|
174 |
torben |
1185 |
imageView.setLayoutParams(new GridView.LayoutParams(thumbSize, thumbSize));
|
175 |
torben |
790 |
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 |
torben |
1013 |
//imageView.setImageURI( Uri.parse(mThumbPaths.get(position)) );
|
183 |
torben |
1223 |
imageView.setImageBitmap( currentEntry.thumb );
|
184 |
torben |
790 |
return imageView;
|
185 |
torben |
925 |
}
|
186 |
|
|
|
187 |
torben |
1185 |
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 |
torben |
1186 |
raf.readFully(buf);
|
194 |
|
|
raf.close();
|
195 |
|
|
|
196 |
torben |
1185 |
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 |
torben |
925 |
class ExtensionFilter implements FilenameFilter {
|
217 |
|
|
private String mExt;
|
218 |
|
|
public ExtensionFilter(String ext) {
|
219 |
|
|
mExt = ext;
|
220 |
|
|
}
|
221 |
torben |
790 |
|
222 |
torben |
925 |
@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 |
torben |
790 |
}
|
235 |
|
|
} |