[英]Android - Save image from internet to internal / external storage and display it in webview
I need to save image from internet to internal / external storage and display it in webview locally. 我需要将图像从Internet保存到内部/外部存储,并在本地的Webview中显示。 I have done retrieve image from internet. 我已经完成了从互联网检索图像。 I need help to save it in internal or external storage and the path to display it in webview. 我需要帮助将其保存在内部或外部存储中以及在Webview中显示它的路径。
How to save image in sdcard from inputstream.. 如何从输入流中将图像保存到sdcard中。
String dir = Environment.getExternalStorageDirectory().toString();
OutputStream fos = null;
File file = new File(dir,"downloadImage.JPEG");
Bitmap bm =BitmapFactory.decodeStream(your input stream);
fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bm.compress(Bitmap.CompressFormat.JPEG, 50, bos);
bos.flush();
bos.close();
Now your image is saved as download.JPEG in sdcard.. 现在,您的图像已另存为download.JPEG在sdcard中。
and you can get image using this path.. 您可以使用此路径获取图像。
dir+"\downloadImage.JPEG"
for internal storage you can use SQlite
or sharedpreference
and for external storage check this tutorial: 对于内部存储,可以使用SQlite
或sharedpreference
;对于外部存储,请查看此教程:
http://www.vogella.de/articles/AndroidFileSystem/article.html http://www.vogella.de/articles/AndroidFileSystem/article.html
try {
FileOutputStream out = new FileOutputStream(filename);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
Fetch image from following function and save drawable in file 从以下功能获取图像并将可绘制对象保存在文件中
private Drawable LoadImageFromWeb(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
System.out.println("Exc=" + e);
return null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.