Android 位图转 Base64 字符串
如何将大位图(用手机摄像头拍摄的照片)转换为 Base64 字符串?
How do I convert a large Bitmap (photo taken with the phone's camera) to a Base64 String?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用以下方法将位图转换为字节数组:
要从字节数组进行 Base64 编码,请使用以下方法
use following method to convert bitmap to byte array:
to encode base64 from byte array use following method
我有快速的解决方案。只需创建一个文件
ImageUtil.java
用法:
或
I have fast solution. Just create a file
ImageUtil.java
Usage:
or
jeet 的答案的问题在于,您将图像的所有字节加载到字节数组中,这可能会使低端设备中的应用程序崩溃。相反,我首先将图像写入文件并使用 Apache 的 Base64InputStream 类。然后,您可以直接从该文件的 InputStream 创建 Base64 字符串。它将如下所示:
正如您所看到的,上述解决方案直接使用流,避免了将所有字节加载到变量中的需要,因此使内存占用量大大降低,并且在低端设备中不太可能崩溃。仍然存在的问题是,将 Base64 字符串本身放入 String 变量并不是一个好主意,因为这可能会导致 OutOfMemory 错误。但至少我们通过消除字节数组将内存消耗减少了一半。
如果您想跳过写入文件步骤,则必须将 OutputStream 转换为 InputStream,这并不是那么简单(您必须使用 PipedInputStream 但这有点复杂,因为两个流必须始终位于不同的线程中)。
The problem with jeet's answer is that you load all bytes of the image into a byte array, which will likely crash the app in low-end devices. Instead, I would first write the image to a file and read it using Apache's Base64InputStream class. Then you can create the Base64 string directly from the InputStream of that file. It will look like this:
As you can see, the above solution works directly with streams instead, avoiding the need to load all the bytes into a variable, therefore making the memory footprint much lower and less likely to crash in low-end devices. There is still the problem that putting the Base64 string itself into a String variable is not a good idea, because, again, it might cause OutOfMemory errors. But at least we have cut the memory consumption by half by eliminating the byte array.
If you want to skip the write-to-a-file step, you have to convert the OutputStream to an InputStream, which is not so straightforward to do (you must use PipedInputStream but that is a little more complex as the two streams must always be in different threads).
尝试一下,首先将图像缩放到所需的宽度和高度,只需将原始位图、所需的宽度和所需的高度传递给以下方法并获取缩放后的位图作为返回:
例如: BitmapscaledBitmap = getScaledBitmap(originalBitmap, 250, 350);
现在只需将缩放后的位图传递给以下方法并获取 Base64 字符串作为返回:
例如: String base64String = getBase64String(scaledBitmap);
要将 Base64 字符串解码回位图图像:
Try this, first scale your image to required width and height, just pass your original bitmap, required width and required height to the following method and get scaled bitmap in return:
For example: Bitmap scaledBitmap = getScaledBitmap(originalBitmap, 250, 350);
Now just pass your scaled bitmap to the following method and get base64 string in return:
For example: String base64String = getBase64String(scaledBitmap);
To decode the base64 string back to bitmap image:
现在大多数人都使用 Kotlin 而不是 Java,下面是 Kotlin 中用于将位图转换为 Base64 字符串的代码。
Now that most people use Kotlin instead of Java, here is the code in Kotlin for converting a bitmap into a base64 string.
所有这些答案都是低效的,因为它们不必要地解码为位图,然后重新压缩位图。当您在 Android 上拍照时,它会以 jpeg 形式存储在您按照 Android 文档。
您应该做的就是直接将该文件转换为 Base64 字符串。
以下是如何通过简单的复制粘贴(在 Kotlin 中)来实现这一点。请注意,您必须关闭 base64FilterStream 才能真正刷新其内部缓冲区。
作为奖励,由于绕过了重新压缩,您的图像质量应该会略有提高。
All of these answers are inefficient as they needlessly decode to a bitmap and then recompress the bitmap. When you take a photo on Android, it is stored as a jpeg in the temp file you specify when you follow the android docs.
What you should do is directly convert that file to a Base64 string.
Here is how to do that in easy copy-paste (in Kotlin). Note you must close the base64FilterStream to truly flush its internal buffer.
As a bonus, your image quality should be slightly improved, due to bypassing the re-compressing.
使用此代码..
Use this code..