将位图保存到位置
我正在开发一个功能,从网络服务器下载图像,将其显示在屏幕上,如果用户希望保留图像,请将其保存在 SD 卡上的某个文件夹中。 有没有一种简单的方法可以获取位图并将其保存到 SD 卡中我选择的文件夹中?
我的问题是我可以下载图像,将其作为位图显示在屏幕上。 我能够找到将图像保存到特定文件夹的唯一方法是使用 FileOutputStream,但这需要一个字节数组。 我不确定如何从位图转换(如果这是正确的方法)到字节数组,所以我可以使用 FileOutputStream 来写入数据。
我的另一个选择是使用 MediaStore :
MediaStore.Images.Media.insertImage(getContentResolver(), bm,
barcodeNumber + ".jpg Card Image", barcodeNumber + ".jpg Card Image");
它可以很好地保存到 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?
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.
The other option I have is to use MediaStore :
MediaStore.Images.Media.insertImage(getContentResolver(), bm,
barcodeNumber + ".jpg Card Image", barcodeNumber + ".jpg Card Image");
Which works fine to save to SD card, but does not allow you to customize the folder.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
您应该使用 Bitmap.compress() 方法将位图保存为文件。 它将压缩(如果使用的格式允许)您的图片并将其推送到 OutputStream 中。
下面是通过
getImageBitmap(myurl)
获取的 Bitmap 实例的示例,该实例可以压缩为 JPEG,压缩率为 85% :You should use the
Bitmap.compress()
method to save a Bitmap as a file. It will compress (if the format used allows it) your picture and push it into an OutputStream.Here is an example of a Bitmap instance obtained through
getImageBitmap(myurl)
that can be compressed as a JPEG with a compression rate of 85% :将在 AndroidManifest.xml 中未经许可抛出异常(至少在 os2.2 中):
will throw exception without permission in AndroidManifest.xml (at least in os2.2):
在
onActivityResult
内部:Inside
onActivityResult
:我知道这个问题已经很老了,但现在我们可以在没有 WRITE_EXTERNAL_STORAGE 权限的情况下获得相同的结果。 我们可以使用文件提供程序代替。
如何从提供商处检索文件?
另外,不要忘记在 Android
manifest
中注册您的provider
I know this question is old, but now we can achieve the same result without
WRITE_EXTERNAL_STORAGE
permission. Instead of we can use File provider.How to retrieve file from provider ?
Also do not forget register your
provider
in Androidmanifest
某些格式(例如无损的 PNG)将忽略质量设置。
Some formats, like PNG which is lossless, will ignore the quality setting.
以下是将位图保存到文件的示例代码:
现在调用此函数将位图保存到内部存储器。
文件newfile = savebitmap(bitmap)
;我希望它能帮助你。
快乐的编码生活。
Here is the sample code for saving bitmap to file :
Now call this function to save the bitmap to internal memory.
File newfile = savebitmap(bitmap)
;I hope it will help you.
Happy codeing life.
为什么不使用 100 来调用 Bitmap.compress 方法(听起来像是无损的)?
Why not call the
Bitmap.compress
method with 100 (which sounds like it is lossless)?我还想保存一张照片。 但我的问题(?)是我想从我绘制的位图中保存它。
我是这样做的:
I would also like to save a picture. But my problem(?) is that I want to save it from a bitmap that ive drawed.
I made it like this:
我发现发送 PNG 和透明度的方式。
The way I found to send PNG and transparency.
将位图保存到您的图库而不压缩。
Save Bitmap to your Gallery Without Compress.
您想要将位图保存到您选择的目录中。 我制作了一个 ImageWorker 库,使用户能够加载、保存和转换位图/绘图/base64 图像。
最小 SDK - 14
先决条件
保存位图/Drawable/Base64
加载位图
实现
添加依赖项
在项目级别 Gradle 中
在应用程序级别 Gradle 中
您可以在 https://github.com/ihimanshurawat/ImageWorker/blob/master/README.md
You want to save Bitmap to Directory of your Choice. I have made a library ImageWorker that enables the user to load, save and convert bitmaps/drawables/base64 images.
Min SDK - 14
Pre-requisite
Saving Bitmap/Drawable/Base64
Loading Bitmap
Implementation
Add Dependencies
In Project Level Gradle
In Application Level Gradle
You can read more on https://github.com/ihimanshurawat/ImageWorker/blob/master/README.md
确保在调用 bitmap.compress 之前创建该目录:
Make sure the directory is created before you call
bitmap.compress
:为视频创建视频缩略图。 如果视频损坏或格式不受支持,它可能会返回 null。
要将位图保存在 SD 卡中,请使用以下代码
存储图像
获取图像存储路径
Create a video thumbnail for a video. It may return null if the video is corrupted or the format is not supported.
To Save your bitmap in sdcard use the following code
Store Image
To Get the Path for Image Storage
确保您已添加下面的权限
并在
xml
文件夹名称 provider_paths.xml 下和 AndroidManifest 下然后只需调用 saveBitmapFile(passYourBitmapHere)
where
其他方法
和
make sure you have added below Permission
and in AndroidManifest under
then simply call saveBitmapFile(passYourBitmapHere)
where
and other methods
and
嘿,只需将名称命名为 .bmp
这样做:
听起来我只是在胡闹,但尝试一下,一旦它会以 bmp 格式保存..干杯
Hey just give the name to .bmp
Do this:
it'll sound that IM JUST FOOLING AROUND but try it once it'll get saved in bmp foramt..Cheers
Android 4.4 Kitkat 之后,截至 2017 年,Android 4.4 及以下版本的份额约为 20%,并且还在下降,无法使用
File
类和getExternalStorageDirectory()
方法。 此方法返回您的设备内存和保存的图像对每个应用程序可见。 您还可以仅保存您的应用程序私有的图像,并在用户使用openFileOutput()
方法删除您的应用程序时将其删除。从 Android 6.0 开始,您可以将 SD 卡格式化为内部存储器,但仅对您的设备私有。(如果您将 SD 汽车格式化为内部存储器,则只有您的设备可以访问或查看其内容)您可以使用以下命令保存到该 SD 卡其他答案,但如果您想使用可移动 SD 卡,您应该阅读下面我的答案。
您应该使用存储访问框架获取文件夹的uri
onActivityResult
活动方法以获取用户选择的文件夹,并添加检索持久权限以便在用户重新启动后能够访问文件夹装置。现在,将保存文件夹保存到共享首选项,而不是每次要保存图像时都要求用户选择文件夹。
您应该使用
DocumentFile
类来保存图像,而不是File
或ParcelFileDescriptor
,有关详细信息,您可以查看 此线程用于使用compress 将图像保存到 SD 卡( CompressFormat.JPEG, 100, out);
方法和DocumentFile
类。After Android 4.4 Kitkat, and as of 2017 share of Android 4.4 and less is about 20% and decreasing, it's not possible to save to SD card using
File
class andgetExternalStorageDirectory()
method. This method returns your device internal memory and images save visible to every app. You can also save images only private to your app and to be deleted when user deletes your app withopenFileOutput()
method.Starting with Android 6.0, you can format your SD card as an internal memory but only private to your device.(If you format SD car as internal memory, only your device can access or see it's contents) You can save to that SD card using other answers but if you want to use a removable SD card you should read my answer below.
You should use Storage Access Framework to get uri to folder
onActivityResult
method of activity to get folder selected by user, and add retreive persistiable permission to be able to access folder after user restarts the device.Now, save save folder to shared preferences not to ask user to select folder every time you want to save an image.
You should use
DocumentFile
class to save your image, notFile
orParcelFileDescriptor
, for more info you can check this thread for saving image to SD card withcompress(CompressFormat.JPEG, 100, out);
method andDocumentFile
classes.// |==| 从位图创建 PNG 文件:
// |==| 从文件获取Bimap:
// |==| Create a PNG File from Bitmap :
// |==| Get Bimap from File :