问题
What I have
I have an "open image dialog" in an activity. This "dialog" only shows folders and compatible images. For this I have a layer with a gridview that I fill with rows that contains an image and a text.
The text is for the name of the file and the image is for a preview of the image.
My problem
The dialog works well if the folder that I'm seeing doesn't have lots of images. I'm working on a SII and when I try to open the camera album (photos of 8Mpx) it goes very slow with my code, even each time a new row is redrawed.
My code
I think the main problem resides in the creation of the preview images because if I delete this part all works fine. For the images, I have:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View grid;
if(convertView==null){
grid = new View(mContext);
LayoutInflater inflater=getLayoutInflater();
grid=inflater.inflate(R.layout.row, parent, false);
}else{
grid = (View)convertView;
}
ImageView icon = (ImageView)grid.findViewById(R.id.file_image);
TextView label = (TextView)grid.findViewById(R.id.file_text);
label.setText(item.get(position));
if (item.get(position).equals("/")){
icon.setImageResource(R.drawable.folderupup);
}else if (item.get(position).endsWith("../")) {
icon.setImageResource(R.drawable.folderup);
}else if (item.get(position).endsWith("/")) {
icon.setImageResource(R.drawable.folder);
}else{
Bitmap b = BitmapFactory.decodeFile(path.get(position));
Bitmap b2 = Bitmap.createScaledBitmap(b, 55, 55, false);
icon.setImageBitmap(b2);
}
return grid;
}
}
回答1:
In somewhere, the code calls this (data is the full pathname of the image):
DiskLruCache.Editor editor = mHttpDiskCache.edit(key);
if (editor != null) {
if (downloadUrlToStream(data,editor.newOutputStream(DISK_CACHE_INDEX))) {
editor.commit();
} else {
editor.abort();
}
}
because I don't know how to change it, I modified the url receptor so now instead of reading from the net it reads from a file:
public boolean downloadUrlToStream(String urlString, OutputStream outputStream) {
disableConnectionReuseIfNecessary();
HttpURLConnection urlConnection = null;
BufferedOutputStream out = null;
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(new File(urlString)), IO_BUFFER_SIZE);
out = new BufferedOutputStream(outputStream, IO_BUFFER_SIZE);
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
return true;
} catch (final IOException e) {
Log.e(TAG, "Error in downloadBitmap - " + e);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (final IOException e) {}
}
return false;
}
But the gridview load the images very slowly. I think it's because the last method. How can I do it better?
回答2:
Finally I did all that the Android page said without doing anything for the cache. Now the list loads fast. Maybe I could add a memory cache as Wenhui pointed but it is so fast that it doesn't matter for my purposes.
The test has been done on a SII with a folder containing around 400 with 8Mpx
来源:https://stackoverflow.com/questions/12789939/open-image-dialog-gridview-is-very-slow