4 |
import java.io.FileOutputStream; |
import java.io.FileOutputStream; |
5 |
import java.io.FilenameFilter; |
import java.io.FilenameFilter; |
6 |
import java.io.IOException; |
import java.io.IOException; |
7 |
|
import java.io.RandomAccessFile; |
8 |
import java.util.ArrayList; |
import java.util.ArrayList; |
9 |
|
import java.util.Collection; |
10 |
|
import java.util.Collections; |
11 |
|
|
12 |
import android.content.Context; |
import android.content.Context; |
13 |
import android.graphics.Bitmap; |
import android.graphics.Bitmap; |
14 |
import android.graphics.BitmapFactory; |
import android.graphics.BitmapFactory; |
15 |
import android.graphics.Bitmap.CompressFormat; |
import android.graphics.Bitmap.CompressFormat; |
|
import android.net.Uri; |
|
16 |
import android.os.Environment; |
import android.os.Environment; |
17 |
import android.util.DisplayMetrics; |
import android.util.DisplayMetrics; |
18 |
import android.util.Log; |
import android.util.Log; |
25 |
import android.widget.Toast; |
import android.widget.Toast; |
26 |
|
|
27 |
public class ImageAdapter extends BaseAdapter { |
public class ImageAdapter extends BaseAdapter { |
28 |
|
|
29 |
|
|
30 |
|
|
31 |
//int mGalleryItemBackground; |
//int mGalleryItemBackground; |
32 |
private Context mContext; |
private Context mContext; |
33 |
|
|
34 |
ArrayList<String> mBitmapPaths = new ArrayList<String>(); |
ArrayList<ImageEntry> mImages = new ArrayList<ImageEntry>(); |
35 |
ArrayList<Bitmap> mBitmaps = new ArrayList<Bitmap>(); |
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
int size; |
int thumbSize; |
40 |
|
|
41 |
public ImageAdapter(Context c) { |
public ImageAdapter(Context c) { |
42 |
mContext = c; |
mContext = c; |
64 |
|
|
65 |
|
|
66 |
for (File f : files) { |
for (File f : files) { |
67 |
|
|
68 |
|
ImageEntry entry = new ImageEntry(); |
69 |
|
entry.path = f.getPath(); |
70 |
|
|
71 |
String fileName = f.getName(); |
String fileName = f.getName(); |
72 |
File thumb = new File ( thumbPath + fileName ); |
File thumb = new File ( thumbPath + fileName ); |
74 |
if (! thumb.exists() || thumb.lastModified() < f.lastModified()) { |
if (! thumb.exists() || thumb.lastModified() < f.lastModified()) { |
75 |
Bitmap bmp = BitmapFactory.decodeFile( f.getPath() ); |
Bitmap bmp = BitmapFactory.decodeFile( f.getPath() ); |
76 |
Bitmap scaled = Bitmap.createScaledBitmap(bmp, bmp.getWidth()/4, bmp.getHeight()/4, true); |
Bitmap scaled = Bitmap.createScaledBitmap(bmp, bmp.getWidth()/4, bmp.getHeight()/4, true); |
77 |
mBitmaps.add(scaled); |
entry.thumb = scaled; |
78 |
try { |
try { |
79 |
FileOutputStream fos = new FileOutputStream(thumb); |
FileOutputStream fos = new FileOutputStream(thumb); |
80 |
scaled.compress(CompressFormat.JPEG, 90, fos); |
scaled.compress(CompressFormat.JPEG, 90, fos); |
85 |
|
|
86 |
} else { |
} else { |
87 |
Bitmap thumbBmp = BitmapFactory.decodeFile( thumb.getPath()); |
Bitmap thumbBmp = BitmapFactory.decodeFile( thumb.getPath()); |
88 |
mBitmaps.add(thumbBmp); |
entry.thumb = thumbBmp; |
89 |
} |
} |
|
mBitmapPaths.add( f.getPath() ); |
|
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 |
size = getSize(); |
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() { |
public int getCount() { |
121 |
return mBitmapPaths.size(); |
return mImages.size(); |
122 |
} |
} |
123 |
|
|
124 |
public Object getItem(int position) { |
public Object getItem(int position) { |
129 |
return position; |
return position; |
130 |
} |
} |
131 |
|
|
132 |
public String getImagePath(int position) { |
public ImageEntry getImageEntry(int position) { |
133 |
return mBitmapPaths.get(position); |
return mImages.get(position); |
134 |
} |
} |
135 |
|
|
136 |
public ArrayList<String> getImagePaths() { |
public ArrayList<ImageEntry> getImages() { |
137 |
return mBitmapPaths; |
return mImages; |
138 |
} |
} |
139 |
|
|
140 |
private int getSize() { |
private int getSize() { |
152 |
ImageView imageView; |
ImageView imageView; |
153 |
if (convertView == null) { // if it's not recycled, initialize some attributes |
if (convertView == null) { // if it's not recycled, initialize some attributes |
154 |
imageView = new ImageView(mContext); |
imageView = new ImageView(mContext); |
155 |
imageView.setLayoutParams(new GridView.LayoutParams(size, size)); |
imageView.setLayoutParams(new GridView.LayoutParams(thumbSize, thumbSize)); |
156 |
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); |
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); |
157 |
imageView.setAdjustViewBounds(true); |
imageView.setAdjustViewBounds(true); |
158 |
imageView.setPadding(4, 4, 4, 4); |
imageView.setPadding(4, 4, 4, 4); |
161 |
} |
} |
162 |
|
|
163 |
//imageView.setImageURI( Uri.parse(mThumbPaths.get(position)) ); |
//imageView.setImageURI( Uri.parse(mThumbPaths.get(position)) ); |
164 |
imageView.setImageBitmap( mBitmaps.get(position) ); |
imageView.setImageBitmap( mImages.get(position).thumb ); |
165 |
return imageView; |
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 { |
class ExtensionFilter implements FilenameFilter { |
196 |
private String mExt; |
private String mExt; |
197 |
public ExtensionFilter(String ext) { |
public ExtensionFilter(String ext) { |