我需要通過URL加載圖像,在ImageView中設置並將文件保存在SD卡中。使用java.net.URL緩慢下載JPG
該類做到這一點,但很慢....的JPG文件具有60KB和下載其3〜6秒我10Mbps的互聯網連接的時間...
我用這加載任何圖像在ListView ...
公共類DownloadImageTask擴展的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){}
}
}
}
謝謝你。現在它在1〜2秒內加載:) –
因此,接受答案(並投票):) – Snicolas