I need to load a image by URL, set in the ImageView and save the file in the SD Card.
This class do that, but very slowly.... The jpg files have 60kb, and the time to download its 3~6 seconds in my 10Mbps internet connection...
I'm using this to load any images in a listview...
public class DownloadImageTask extends AsyncTask {
ImageView bmImage;
String path;
String filename = "pg0.rsc";
String param;
Context context;
public DownloadImageTask(Context context, ImageView bmImage, String param, String code) {
this.bmImage = bmImage;
this.param = param;
this.path = Environment.getExternalStorageDirectory().toString() + "/.RascunhoCache/." + code + "/";
this.context = context;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
if(param.equals("c")){
OutputStream outStream = null;
new File(path).mkdirs();
File file = new File(path, filename);
try {
outStream = new FileOutputStream(file);
result.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
}
catch(Exception e){}
}
}
}