将base64字符串转换为位图的Android代码 [英] Android code to convert base64 string to bitmap
问题描述
您好 stackoverflow 团队,我在将 base64 字符串转换为 android 中的位图时遇到问题.我正在使用相机获取图像,并将图像转换为 base64 字符串以发布到服务器.我想在 imageview 中显示该图像,所以在从相机获取图像后如何在 ImageView 中显示图像.请帮我解决问题.
Hi stackoverflow team i have a problem in converting base64 string to bitmap in android. I am using the camera to fetch the image and i am convert the image to base64 string to post to the server. I want to show that image in the imageview so how can i show the image in the ImageView after fetching the image from the camera. please help me to solve the problem.
推荐答案
假设您的图像数据在一个名为 myImageData 的字符串中,以下应该可以解决问题:
Assuming that your image data is in a String called myImageData, the following should do the trick:
byte[] imageAsBytes = Base64.decode(myImageData.getBytes(), Base64.DEFAULT);
ImageView image = (ImageView)this.findViewById(R.id.ImageView);
image.setImageBitmap(
BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
);
对于 Base64 解码,您可以使用 http://iharder.sourceforge.net/current/java/base64/ 因为 Android 在 2.2 之前不包含 Base64 支持.
For Base64 decoding, you can use http://iharder.sourceforge.net/current/java/base64/ as Android doesn't contain Base64-support prior to 2.2.
请注意,我实际上并没有运行此代码,因此您必须仔细检查错误.
Note, I didn't actually run this code, so you'll have to doublecheck for errors.
这篇关于将base64字符串转换为位图的Android代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!