我如何保存从URL的形象? [英] How can I save an image from a url?

查看:126
本文介绍了我如何保存从URL的形象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置使用 setImageBitmap 与外部图像的URL的ImageView的。我想保存图像,这样就可以在以后即使没有互联网连接使用。在哪里以及如何保存呢?

I'm setting an ImageView using setImageBitmap with an external image url. I would like to save the image so it can be used later on even if there is no internet connection. Where and how can I save it?

推荐答案

您必须将它保存在SD卡或你的包数据,因为在运行时,你只能访问这些。要做到这一点,这是一个很好的例子。

You have to save it in SD card or in your package data, because on runtime you only have access to these. To do that this is a good example

URL url = new URL ("file://some/path/anImage.png");
InputStream input = url.openStream();
try {
//The sdcard directory e.g. '/sdcard' can be used directly, or 
//more safely abstracted with getExternalStorageDirectory()
File storagePath = Environment.getExternalStorageDirectory();
OutputStream output = new FileOutputStream (storagePath + "/myImage.png");
try {
    byte[] buffer = new byte[aReasonableSize];
    int bytesRead = 0;
    while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
        output.write(buffer, 0, bytesRead);
    }
} finally {
    output.close();
}
} finally {
input.close();
}

来源:<一个href=\"http://stackoverflow.com/questions/3296850/android-how-to-store-images-from-url-save-it-in-sd-card\">How我从它的URL传送一个图像到SD卡?

这篇关于我如何保存从URL的形象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆