android bitmap string,Android Bitmap到Base64字符串(Android Bitmap to Base64 String)

这篇博客介绍了如何在Android中将一个大尺寸的Bitmap(例如,通过手机相机拍摄的照片)转换成Base64字符串。首先,使用Bitmap的compress方法将其转换为字节数组,然后利用Base64的encodeToString方法编码为Base64格式。这个过程对于在网络上传输或者存储图像数据非常有用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android Bitmap到Base64字符串(Android Bitmap to Base64 String)

如何将一个大的Bitmap(用手机相机拍摄的照片)转换为Base64 String?

How do I convert a large Bitmap (photo taken with the phone's camera) to a Base64 String?

原文:https://stackoverflow.com/questions/9224056

更新时间:2019-11-21 18:31

最满意答案

使用以下方法将位图转换为字节数组:

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);

byte[] byteArray = byteArrayOutputStream .toByteArray();

从字节数组中使用以下方法对base64进行编码

String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);

use following method to convert bitmap to byte array:

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);

byte[] byteArray = byteArrayOutputStream .toByteArray();

to encode base64 from byte array use following method

String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);

2013-12-12

相关问答

假设您的图像数据位于一个名为myImageData的字符串中,以下内容应该可以做到这一点: byte[] imageAsBytes = Base64.decode(myImageData.getBytes(), Base64.DEFAULT);

ImageView image = (ImageView)this.findViewById(R.id.ImageView);

image.setImageBitmap(

BitmapFactory.deco

...

public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality)

{

ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();

image.compress(compressFormat, quality, byteArrayOS);

return Base64.enc

...

使用以下方法将位图转换为字节数组: ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);

byte[] byteArray = byteArrayOutputStream .toByteArray();

从字节数组中使用以下方法对base64进行编码 St

...

您只需使用其他内置方法即可恢复代码。 byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);

Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

You can just basically revert your code using some other built in meth

...

我发现了问题。 问题在于logcat来自我复制编码字符串的地方。 Logcat没有显示整个String,因此破碎的字符串没有为我解码图像。 因此,当我将编码的字符串直接传递给解码函数时,图像是可见的。 I have found out the problem. The problem was with the logcat from where i was copying the encoded string. Logcat didn't display the entire String so

...

试试这个位图; public Bitmap convert(String img){

byte[] b = Base64.decode(img, Base64.DEFAULT);

return BitmapFactory.decodeByteArray(b, 0, b.length);

}

而这就是String public String convert(Bitmap bm, int quality){

ByteArrayOutputStream baos = n

...

表示图像的前三个base64字符串解码正常。 但是接下来的四个产生了一个bad base-64在线的捕获 byte[] decodedString = Base64.decode(photo, Base64.URL_SAFE);

之前我说过。 如果你愿意的话 byte[] decodedString = Base64.decode(photo, Base64.DEFAULT);

然后没有捕获。 所有7个base64字符串解码都可以。 The first three base64 strings

...

如果你已经在文件中有了它,那么就没有理由对它进行解压缩然后重新压缩 - 这可能是导致错误的原因,因为每次压缩都是有损的并且会导致数据进一步丢失。 如果您已有图像文件,请将其作为原始字节和Base64读取。 If you already have it in the file, there is no reason to decompress it then recompress it- which may be the cause of your errors, as each compressi

...

你可以使用httpmime库上传任何文件(图像,音频,视频等..)请参考下面的代码。 HttpClient httpClient = new DefaultHttpClient();

HttpContext localContext = new BasicHttpContext();

HttpPost postRequest = new HttpPost(your url);

MultipartEntity reqEntity = new MultipartEntity(

Htt

...

只需将base64字符串转换为位图,而不是使用下面的代码将该位图加载到imageview中 byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);

Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

image.setImageBitmap(decodedByte);

Just con

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值