将位图保存到位置 [英] Save bitmap to location

查看:43
本文介绍了将位图保存到位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一种功能,可以从 Web 服务器下载图像,将其显示在屏幕上,如果用户希望保留该图像,请将其保存在 SD 卡上的某个文件夹中.有没有一种简单的方法来获取位图并将其保存到我选择的文件夹中的 SD 卡中?

I am working on a function to download an image from a web server, display it on the screen, and if the user wishes to keep the image, save it on the SD card in a certain folder. Is there an easy way to take a bitmap and just save it to the SD card in a folder of my choice?

我的问题是我可以下载图像,将其作为位图显示在屏幕上.我能够找到将图像保存到特定文件夹的唯一方法是使用 FileOutputStream,但这需要一个字节数组.我不确定如何将(如果这甚至是正确的方式)从 Bitmap 转换为字节数组,因此我可以使用 FileOutputStream 来写入数据.

My issue is that I can download the image, display it on screen as a Bitmap. The only way I have been able to find to save an image to a particular folder is to use FileOutputStream, but that requires a byte array. I am not sure how to convert (if this is even the right way) from Bitmap to byte array, so I can use a FileOutputStream to write the data.

我的另一个选择是使用 MediaStore :

The other option I have is to use MediaStore :

MediaStore.Images.Media.insertImage(getContentResolver(), bm,
    barcodeNumber + ".jpg Card Image", barcodeNumber + ".jpg Card Image");

保存到 SD 卡可以正常工作,但不允许您自定义文件夹.

Which works fine to save to SD card, but does not allow you to customize the folder.

推荐答案

try (FileOutputStream out = new FileOutputStream(filename)) {
    bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
    // PNG is a lossless format, the compression factor (100) is ignored
} catch (IOException e) {
    e.printStackTrace();
}

这篇关于将位图保存到位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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