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.util.ArrayList;
|
8 |
|
9 |
import android.content.Context;
|
10 |
import android.graphics.Bitmap;
|
11 |
import android.graphics.BitmapFactory;
|
12 |
import android.graphics.Bitmap.CompressFormat;
|
13 |
import android.net.Uri;
|
14 |
import android.os.Environment;
|
15 |
import android.util.DisplayMetrics;
|
16 |
import android.util.Log;
|
17 |
import android.view.View;
|
18 |
import android.view.ViewGroup;
|
19 |
import android.view.WindowManager;
|
20 |
import android.widget.BaseAdapter;
|
21 |
import android.widget.GridView;
|
22 |
import android.widget.ImageView;
|
23 |
import android.widget.Toast;
|
24 |
|
25 |
public class ImageAdapter extends BaseAdapter {
|
26 |
//int mGalleryItemBackground;
|
27 |
private Context mContext;
|
28 |
|
29 |
ArrayList<String> mBitmapPaths = new ArrayList<String>();
|
30 |
ArrayList<Bitmap> mBitmaps = new ArrayList<Bitmap>();
|
31 |
|
32 |
int size;
|
33 |
|
34 |
public ImageAdapter(Context c) {
|
35 |
mContext = c;
|
36 |
|
37 |
String path = Environment.getExternalStorageDirectory().getPath() + "/Side9" ;
|
38 |
|
39 |
|
40 |
|
41 |
File root = new File(path);
|
42 |
|
43 |
if (root.exists() == false) {
|
44 |
Toast.makeText(mContext, "Side9 folder not found on sdcard", Toast.LENGTH_LONG).show();
|
45 |
return;
|
46 |
}
|
47 |
|
48 |
File files[] = root.listFiles( new ExtensionFilter("jpg") );
|
49 |
|
50 |
|
51 |
String thumbPath = path + "/.thumb/";
|
52 |
|
53 |
File thumbDir = new File(thumbPath);
|
54 |
if (! thumbDir.exists()) {
|
55 |
thumbDir.mkdirs();
|
56 |
}
|
57 |
|
58 |
|
59 |
for (File f : files) {
|
60 |
|
61 |
String fileName = f.getName();
|
62 |
File thumb = new File ( thumbPath + fileName );
|
63 |
|
64 |
if (! thumb.exists()) {
|
65 |
Bitmap bmp = BitmapFactory.decodeFile( f.getPath() );
|
66 |
Bitmap scaled = Bitmap.createScaledBitmap(bmp, bmp.getWidth()/4, bmp.getHeight()/4, true);
|
67 |
mBitmaps.add(scaled);
|
68 |
try {
|
69 |
FileOutputStream fos = new FileOutputStream(thumb);
|
70 |
scaled.compress(CompressFormat.JPEG, 90, fos);
|
71 |
|
72 |
} catch (IOException e) {
|
73 |
Log.e("Side9", "error", e);
|
74 |
}
|
75 |
|
76 |
} else {
|
77 |
Bitmap thumbBmp = BitmapFactory.decodeFile( thumb.getPath());
|
78 |
mBitmaps.add(thumbBmp);
|
79 |
}
|
80 |
mBitmapPaths.add( f.getPath() );
|
81 |
|
82 |
}
|
83 |
|
84 |
size = getSize();
|
85 |
}
|
86 |
|
87 |
public int getCount() {
|
88 |
return mBitmapPaths.size();
|
89 |
}
|
90 |
|
91 |
public Object getItem(int position) {
|
92 |
return position;
|
93 |
}
|
94 |
|
95 |
public long getItemId(int position) {
|
96 |
return position;
|
97 |
}
|
98 |
|
99 |
public String getImagePath(int position) {
|
100 |
return mBitmapPaths.get(position);
|
101 |
}
|
102 |
|
103 |
public ArrayList<String> getImagePaths() {
|
104 |
return mBitmapPaths;
|
105 |
}
|
106 |
|
107 |
private int getSize() {
|
108 |
DisplayMetrics metrics = new DisplayMetrics();
|
109 |
WindowManager wmgr = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
|
110 |
wmgr.getDefaultDisplay().getMetrics(metrics);
|
111 |
if (metrics.widthPixels < 480) {
|
112 |
return 150;
|
113 |
} else {
|
114 |
return 200;
|
115 |
}
|
116 |
}
|
117 |
|
118 |
public View getView(int position, View convertView, ViewGroup parent) {
|
119 |
ImageView imageView;
|
120 |
if (convertView == null) { // if it's not recycled, initialize some attributes
|
121 |
imageView = new ImageView(mContext);
|
122 |
imageView.setLayoutParams(new GridView.LayoutParams(size, size));
|
123 |
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
|
124 |
imageView.setAdjustViewBounds(true);
|
125 |
imageView.setPadding(4, 4, 4, 4);
|
126 |
} else {
|
127 |
imageView = (ImageView) convertView;
|
128 |
}
|
129 |
|
130 |
//imageView.setImageURI( Uri.parse(mThumbPaths.get(position)) );
|
131 |
imageView.setImageBitmap( mBitmaps.get(position) );
|
132 |
return imageView;
|
133 |
}
|
134 |
|
135 |
class ExtensionFilter implements FilenameFilter {
|
136 |
private String mExt;
|
137 |
public ExtensionFilter(String ext) {
|
138 |
mExt = ext;
|
139 |
}
|
140 |
|
141 |
@Override
|
142 |
public boolean accept(File dir, String filename) {
|
143 |
String parts[] = filename.split("\\.");
|
144 |
if (parts.length > 1) {
|
145 |
String ext = parts[ parts.length -1];
|
146 |
if (ext.equals(mExt)) {
|
147 |
return true;
|
148 |
}
|
149 |
}
|
150 |
return false;
|
151 |
}
|
152 |
|
153 |
}
|
154 |
} |