/[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 1185 - (show annotations) (download)
Tue Nov 2 17:08:57 2010 UTC (13 years, 6 months ago) by torben
File size: 5890 byte(s)
Add options for sorting the images

Bumb version to 17
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
82 } catch (IOException e) {
83 Log.e("Side9", "error", e);
84 }
85
86 } else {
87 Bitmap thumbBmp = BitmapFactory.decodeFile( thumb.getPath());
88 entry.thumb = thumbBmp;
89 }
90
91 /* load captions */
92 String infoFileName = f.getPath().replace(".jpg", ".txt");
93
94 File infoFile = new File(infoFileName);
95 if (infoFile.exists() ) {
96 entry.caption = cleanString( getFileContent(infoFile) );
97 } else {
98 entry.caption = "";
99 }
100
101 mImages.add(entry);
102 }
103
104 Collections.sort(mImages, new ImageEntry.PathComparator() );
105
106 thumbSize = getSize();
107 }
108
109 public void orderByPath() {
110 Collections.sort(mImages, new ImageEntry.PathComparator());
111 this.notifyDataSetChanged();
112 }
113
114 public void orderByCaption() {
115 Collections.sort(mImages, new ImageEntry.CaptionComparator());
116 this.notifyDataSetChanged();
117 }
118
119
120 public int getCount() {
121 return mImages.size();
122 }
123
124 public Object getItem(int position) {
125 return position;
126 }
127
128 public long getItemId(int position) {
129 return position;
130 }
131
132 public ImageEntry getImageEntry(int position) {
133 return mImages.get(position);
134 }
135
136 public ArrayList<ImageEntry> getImages() {
137 return mImages;
138 }
139
140 private int getSize() {
141 DisplayMetrics metrics = new DisplayMetrics();
142 WindowManager wmgr = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
143 wmgr.getDefaultDisplay().getMetrics(metrics);
144 if (metrics.widthPixels < 480) {
145 return 150;
146 } else {
147 return 200;
148 }
149 }
150
151 public View getView(int position, View convertView, ViewGroup parent) {
152 ImageView imageView;
153 if (convertView == null) { // if it's not recycled, initialize some attributes
154 imageView = new ImageView(mContext);
155 imageView.setLayoutParams(new GridView.LayoutParams(thumbSize, thumbSize));
156 imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
157 imageView.setAdjustViewBounds(true);
158 imageView.setPadding(4, 4, 4, 4);
159 } else {
160 imageView = (ImageView) convertView;
161 }
162
163 //imageView.setImageURI( Uri.parse(mThumbPaths.get(position)) );
164 imageView.setImageBitmap( mImages.get(position).thumb );
165 return imageView;
166 }
167
168 String getFileContent(File filename) {
169 try {
170 RandomAccessFile raf = new RandomAccessFile(filename, "r");
171 int size = (int)raf.length();
172 byte buf[] = new byte[size];
173
174 raf.readFully(buf);
175 return new String(buf);
176 } catch (IOException e) {
177 Log.e("Side9", "Error", e);
178 return "";
179 }
180 }
181
182 String cleanString(String input) { //trim and remove double spaces
183 String res = input.trim();
184 while ( res.contains( " ")) {
185 res = res.replace(" ", " ");
186 }
187
188 return res;
189 }
190
191
192
193
194
195 class ExtensionFilter implements FilenameFilter {
196 private String mExt;
197 public ExtensionFilter(String ext) {
198 mExt = ext;
199 }
200
201 @Override
202 public boolean accept(File dir, String filename) {
203 String parts[] = filename.split("\\.");
204 if (parts.length > 1) {
205 String ext = parts[ parts.length -1];
206 if (ext.equals(mExt)) {
207 return true;
208 }
209 }
210 return false;
211 }
212
213 }
214 }

  ViewVC Help
Powered by ViewVC 1.1.20