将从 InputStream 检索到的图像保存在本地
我使用以下代码从 url 检索图像并将其显示在活动中。
InputStream is = (InputStream) new URL(url[0]).getContent();
Drawable d = Drawable.createFromStream(is, "imagename");
ImageView...
现在,当用户单击按钮时,我想在本地保存此图像(Drawable d),以便我可以在另一个活动中再次显示它(以及其他一些任务)。
我想将其存储在应用程序文件夹本身而不是 SD 卡上。
我该怎么做?
谢谢! 香农
I used the following code to retrieve an image from a url and display it within an activity.
InputStream is = (InputStream) new URL(url[0]).getContent();
Drawable d = Drawable.createFromStream(is, "imagename");
ImageView...
Now I want to save this image (Drawable d) locally when a user clicks a button so I can display it again in another activity (along with a few other tasks).
I'd like to store it within the app folder itself rather than on the SD card.
How would I do this?
Thanks!
Shannon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这将为您做:
This will do it for you:
对于 Drawable 保存为图像,我正在这样做,
但实际上,我建议您不要从 InputStream 到 Drawable,而是从 InputStream 到文件,然后从文件中加载图像。因此,您可以保存第一个文件并在加载图像时使用它。
对于 url Inputstream 写入文件,请参阅本教程 从 URL 保存二进制文件
For Drawable save as image, I am doing this,
But Actually, I suggest you to Instead of going from an InputStream to a Drawable, go from an InputStream to a File, then load the image out of the file. So you can save first file and use it in loading Image.
And for url Inputstream to write file look at this tutorial Save binary file from URL
请查看此文档了解如何保存缓存文件和此文档用于一般内部文件存储。
Please view this documentation for how to save cached files and this documentation for general internal file storage.
测试片段:
Tested snippet :