将从 InputStream 检索到的图像保存在本地

发布于 2024-12-13 14:45:18 字数 341 浏览 2 评论 0原文

我使用以下代码从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月下凄凉 2024-12-20 14:45:18

这将为您做:

Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
FileOutputStream out = openFileOutput(filename, Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);

This will do it for you:

Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
FileOutputStream out = openFileOutput(filename, Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
爱要勇敢去追 2024-12-20 14:45:18

对于 Drawable 保存为图像,我正在这样做,

Bitmap image_saved=BitmapFactory.decodeResource(context.getResources(),R.drawable.icon);

FileOutputStream fOut=new FileOutputStream(path+"/"+fileName); 
// Here path is either sdcard or internal storage
image_saved.compress(Bitmap.CompressFormat.JPEG,100,fOut);
fOut.flush();
fOut.close();
image_saved.recycle(); // If no longer used..

但实际上,我建议您不要从 InputStream 到 Drawable,而是从 InputStream 到文件,然后从文件中加载图像。因此,您可以保存第一个文件并在加载图像时使用它。

对于 url Inputstream 写入文件,请参阅本教程 从 URL 保存二进制文件

For Drawable save as image, I am doing this,

Bitmap image_saved=BitmapFactory.decodeResource(context.getResources(),R.drawable.icon);

FileOutputStream fOut=new FileOutputStream(path+"/"+fileName); 
// Here path is either sdcard or internal storage
image_saved.compress(Bitmap.CompressFormat.JPEG,100,fOut);
fOut.flush();
fOut.close();
image_saved.recycle(); // If no longer used..

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

千年*琉璃梦 2024-12-20 14:45:18

请查看此文档了解如何保存缓存文件和此文档用于一般内部文件存储。

Please view this documentation for how to save cached files and this documentation for general internal file storage.

爱的那么颓废 2024-12-20 14:45:18

测试片段:

InputStream is=null;
            try {
                is = (InputStream) new URL(url).getContent();
            } catch (MalformedURLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            Drawable d = Drawable.createFromStream(is, "profile_picture");
            Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
            FileOutputStream out=null;
            try {
                out = getActivity().getApplicationContext().openFileOutput("profile_picture", getActivity().getApplicationContext().MODE_PRIVATE);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);

Tested snippet :

InputStream is=null;
            try {
                is = (InputStream) new URL(url).getContent();
            } catch (MalformedURLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            Drawable d = Drawable.createFromStream(is, "profile_picture");
            Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
            FileOutputStream out=null;
            try {
                out = getActivity().getApplicationContext().openFileOutput("profile_picture", getActivity().getApplicationContext().MODE_PRIVATE);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文