如何从URL加载图像并将其保存在Android设备的内存中?不要说我使用毕加索或oser laibrary。 我需要: 如果设备获得互联网连接我从URL加载图像到ImageView并将其保存在设备的内存中,否则我需要将其中一个保存图像加载到imageView。感谢`的帮助从URL加载图像并保存在内存中android
P.S.对不起,我可以犯一些错误,因为我不太熟悉英语。 这是我的类:
public class ImageManager {
String file_path;
Bitmap bitmap = null;
public Bitmap donwoaledImageFromSD() {
File image = new File(Environment.getExternalStorageDirectory().getPath(),file_path);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
return bitmap;
}
private void savebitmap() {
File file = new File("first");
file_path = file.getAbsolutePath();
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90,fileOutputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
public void fetchImage(final String url, final ImageView iView) {
new AsyncTask<String, Void, Bitmap>() {
protected Bitmap doInBackground(String... iUrl) {
try {
InputStream in = new URL(url).openStream();
bitmap = BitmapFactory.decodeStream(in);
savebitmap();
} catch (Exception e) {
donwoaledImageFromSD();
}
return bitmap;
}
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
if (iView != null) {
iView.setImageBitmap(result);
}
}
}.execute(url);
}
}
Ÿ不ü使用像滑翔可靠库,Picaso等 –
它是不准使用标准库(Glide,Picaso等)的标准之一 –
请提供一个[mcve]演示你已经尝试了什么,以及你遇到了什么具体问题。 – CommonsWare