/[projects]/android/Side9/src/dk/thoerup/side9/ImageAdapter.java
ViewVC logotype

Annotation of /android/Side9/src/dk/thoerup/side9/ImageAdapter.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1185 - (hide 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 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 1185
29    
30    
31 torben 790 //int mGalleryItemBackground;
32     private Context mContext;
33    
34 torben 1185 ArrayList<ImageEntry> mImages = new ArrayList<ImageEntry>();
35 torben 915
36 torben 1185
37    
38    
39     int thumbSize;
40 torben 790
41     public ImageAdapter(Context c) {
42     mContext = c;
43    
44     String path = Environment.getExternalStorageDirectory().getPath() + "/Side9" ;
45    
46 torben 912
47    
48 torben 790 File root = new File(path);
49 torben 912
50     if (root.exists() == false) {
51     Toast.makeText(mContext, "Side9 folder not found on sdcard", Toast.LENGTH_LONG).show();
52     return;
53     }
54 torben 925
55     File files[] = root.listFiles( new ExtensionFilter("jpg") );
56 torben 912
57 torben 1013
58     String thumbPath = path + "/.thumb/";
59    
60     File thumbDir = new File(thumbPath);
61     if (! thumbDir.exists()) {
62     thumbDir.mkdirs();
63 torben 790 }
64 torben 915
65 torben 1013
66     for (File f : files) {
67 torben 1185
68     ImageEntry entry = new ImageEntry();
69     entry.path = f.getPath();
70 torben 1013
71     String fileName = f.getName();
72     File thumb = new File ( thumbPath + fileName );
73    
74 torben 1179 if (! thumb.exists() || thumb.lastModified() < f.lastModified()) {
75 torben 1013 Bitmap bmp = BitmapFactory.decodeFile( f.getPath() );
76     Bitmap scaled = Bitmap.createScaledBitmap(bmp, bmp.getWidth()/4, bmp.getHeight()/4, true);
77 torben 1185 entry.thumb = scaled;
78 torben 1013 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 torben 1185 entry.thumb = thumbBmp;
89 torben 1013 }
90    
91 torben 1185 /* 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 torben 1013 }
103    
104 torben 1185 Collections.sort(mImages, new ImageEntry.PathComparator() );
105    
106     thumbSize = getSize();
107 torben 790 }
108 torben 1185
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 torben 790
120     public int getCount() {
121 torben 1185 return mImages.size();
122 torben 790 }
123    
124     public Object getItem(int position) {
125     return position;
126     }
127    
128     public long getItemId(int position) {
129     return position;
130     }
131    
132 torben 1185 public ImageEntry getImageEntry(int position) {
133     return mImages.get(position);
134 torben 790 }
135 torben 915
136 torben 1185 public ArrayList<ImageEntry> getImages() {
137     return mImages;
138 torben 925 }
139    
140 torben 915 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 torben 790
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 torben 1185 imageView.setLayoutParams(new GridView.LayoutParams(thumbSize, thumbSize));
156 torben 790 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 torben 1013 //imageView.setImageURI( Uri.parse(mThumbPaths.get(position)) );
164 torben 1185 imageView.setImageBitmap( mImages.get(position).thumb );
165 torben 790 return imageView;
166 torben 925 }
167    
168 torben 1185 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 torben 925 class ExtensionFilter implements FilenameFilter {
196     private String mExt;
197     public ExtensionFilter(String ext) {
198     mExt = ext;
199     }
200 torben 790
201 torben 925 @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 torben 790 }
214     }

  ViewVC Help
Powered by ViewVC 1.1.20