In my application there is a set amount of images that need cached. It is about 12 – 15 images I will need cached tops. What I have done is store the image in a file in a directory on the device like this.
FileOutputStream o = new FileOutputStream(getFilesDir().getAbsolutePath()
+ "/background.txt");
Bitmap b = getBitmapFromURL("http://10.84.4.2:8083/images/General/background.png");
b.compress(Bitmap.CompressFormat.PNG, 90, o);
Then just reload the image when I need it like this
BitmapDrawable bg = new BitmapDrawable(BitmapFactory.decodeFile(c
.getFilesDir().getAbsolutePath() + "/background.txt"));
I download the images all at once using Asynch Task like this
public class DownloadImagesTask extends AsyncTask<String, String, Bitmap>
{
Bitmap bmp;
String name;
@Override
protected Bitmap doInBackground(String... urls)
{
name = urls[1];
return download_Image(urls[0]);
}
@Override
protected void onPostExecute(Bitmap result)
{
try
{
FileOutputStream o = new FileOutputStream(getFilesDir()
.getAbsolutePath() + name);
result.compress(Bitmap.CompressFormat.PNG, 90, o);
} catch (Exception e)
{
e.printStackTrace();
}
}
private Bitmap download_Image(String url)
{
return bmp = getBitmapFromURL(url);
}
}
My question is, is this a bad way of doing this? I tried finding good tutorials about caching online, but found it difficult to find a full tutorial that actually explained it properly. The method I am using seems quite simple, but not sure if it is recommended.
Would be grateful if anyone could tell me what are the pros and cons of doing it my way, and should I consider changing to another method.
Thanks
try using FedorVlasov’s image loader from the lazyadapter project