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 |
790 |
import java.util.ArrayList;
|
8 |
|
|
|
9 |
|
|
import android.content.Context;
|
10 |
|
|
import android.graphics.Bitmap;
|
11 |
|
|
import android.graphics.BitmapFactory;
|
12 |
torben |
1013 |
import android.graphics.Bitmap.CompressFormat;
|
13 |
|
|
import android.net.Uri;
|
14 |
torben |
790 |
import android.os.Environment;
|
15 |
torben |
915 |
import android.util.DisplayMetrics;
|
16 |
torben |
1013 |
import android.util.Log;
|
17 |
torben |
790 |
import android.view.View;
|
18 |
|
|
import android.view.ViewGroup;
|
19 |
torben |
915 |
import android.view.WindowManager;
|
20 |
torben |
790 |
import android.widget.BaseAdapter;
|
21 |
|
|
import android.widget.GridView;
|
22 |
|
|
import android.widget.ImageView;
|
23 |
torben |
912 |
import android.widget.Toast;
|
24 |
torben |
790 |
|
25 |
|
|
public class ImageAdapter extends BaseAdapter {
|
26 |
|
|
//int mGalleryItemBackground;
|
27 |
|
|
private Context mContext;
|
28 |
|
|
|
29 |
torben |
791 |
ArrayList<String> mBitmapPaths = new ArrayList<String>();
|
30 |
|
|
ArrayList<Bitmap> mBitmaps = new ArrayList<Bitmap>();
|
31 |
torben |
915 |
|
32 |
|
|
int size;
|
33 |
torben |
790 |
|
34 |
|
|
public ImageAdapter(Context c) {
|
35 |
|
|
mContext = c;
|
36 |
|
|
|
37 |
|
|
String path = Environment.getExternalStorageDirectory().getPath() + "/Side9" ;
|
38 |
|
|
|
39 |
torben |
912 |
|
40 |
|
|
|
41 |
torben |
790 |
File root = new File(path);
|
42 |
torben |
912 |
|
43 |
|
|
if (root.exists() == false) {
|
44 |
|
|
Toast.makeText(mContext, "Side9 folder not found on sdcard", Toast.LENGTH_LONG).show();
|
45 |
|
|
return;
|
46 |
|
|
}
|
47 |
torben |
925 |
|
48 |
|
|
File files[] = root.listFiles( new ExtensionFilter("jpg") );
|
49 |
torben |
912 |
|
50 |
torben |
1013 |
|
51 |
|
|
String thumbPath = path + "/.thumb/";
|
52 |
|
|
|
53 |
|
|
File thumbDir = new File(thumbPath);
|
54 |
|
|
if (! thumbDir.exists()) {
|
55 |
|
|
thumbDir.mkdirs();
|
56 |
torben |
790 |
}
|
57 |
torben |
915 |
|
58 |
torben |
1013 |
|
59 |
|
|
for (File f : files) {
|
60 |
|
|
|
61 |
|
|
String fileName = f.getName();
|
62 |
|
|
File thumb = new File ( thumbPath + fileName );
|
63 |
|
|
|
64 |
torben |
1179 |
if (! thumb.exists() || thumb.lastModified() < f.lastModified()) {
|
65 |
torben |
1013 |
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 |
torben |
915 |
size = getSize();
|
85 |
torben |
790 |
}
|
86 |
|
|
|
87 |
|
|
public int getCount() {
|
88 |
torben |
1013 |
return mBitmapPaths.size();
|
89 |
torben |
790 |
}
|
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 |
torben |
791 |
return mBitmapPaths.get(position);
|
101 |
torben |
790 |
}
|
102 |
torben |
915 |
|
103 |
torben |
925 |
public ArrayList<String> getImagePaths() {
|
104 |
|
|
return mBitmapPaths;
|
105 |
|
|
}
|
106 |
|
|
|
107 |
torben |
915 |
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 |
torben |
790 |
|
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 |
torben |
915 |
imageView.setLayoutParams(new GridView.LayoutParams(size, size));
|
123 |
torben |
790 |
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 |
torben |
1013 |
//imageView.setImageURI( Uri.parse(mThumbPaths.get(position)) );
|
131 |
torben |
791 |
imageView.setImageBitmap( mBitmaps.get(position) );
|
132 |
torben |
790 |
return imageView;
|
133 |
torben |
925 |
}
|
134 |
|
|
|
135 |
|
|
class ExtensionFilter implements FilenameFilter {
|
136 |
|
|
private String mExt;
|
137 |
|
|
public ExtensionFilter(String ext) {
|
138 |
|
|
mExt = ext;
|
139 |
|
|
}
|
140 |
torben |
790 |
|
141 |
torben |
925 |
@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 |
torben |
790 |
}
|
154 |
|
|
} |