I have written a bit of code that when the listview is clicked, the image at that location is stored to external memory, then the file path string is sent with the intent to view the image in the default gallery. The only problem is that it takes a seriously long amount of time (I’m talking 10+ seconds on my thunderbolt).
What I haved tried:
1. Storing the bitmap on internal memory
2. Lowering the quality of the bitmap
Here is the code:
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
if(position>0){
Bitmap bmp =adapter.getBitmap(adapter.getData(position-1));
if(bmp!=null){
//String path = context.getCacheDir().getAbsolutePath() + "/view.png";
//File f = new File(context.getCacheDir().getAbsolutePath(),"MemeCache");
//if(!f.exists())
// f.mkdirs();
String path = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/view.png";
Toast.makeText(context, "opening in gallery", Toast.LENGTH_SHORT).show();
File file = new File(path);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
bmp.compress(CompressFormat.PNG, 100, fos );
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path)), "image/png");
//intent.setDataAndType(Uri.fromFile(f), "image/png");
activity.startActivity(intent);
}else{
Toast.makeText(context, arg0.getItemAtPosition(position).toString() +"is HaAAACkSS!!!!", Toast.LENGTH_SHORT).show();
}
}
}
});
Some things that might help:
1) add some timers or instrumentation to you code to see exactly where you’re spending all the time: saving the bitmap to the sdcard, starting the intent, or something else entirely.
2) Once you’ve added timers and can measure the performance, you can see if shrinking the image or storing the image in a different place helps and if so how much. On some devices, internal storage is on the sdcard itself.
3) depending on your program, you might want to consider pre-saving the files to the sdcard (possibly in the background) so that they are probably already saved by the time the user tries to view them in the gallery.