安卓code转换成Base64的字符串作为位图位图、转换成、字符串、code

2023-09-13 23:49:12 作者:烟云似雪

嗨计算器队我有转换的base64字符串中的Andr​​oid为位图的一个问题。我现在用的摄像头获取的图像,我将图像转换为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不包含的Base64支持2.2之前。

For Base64 decoding, you can use http://iharder.sourceforge.net/current/java/base64/ as Android doesn't contain Base64-support prior to 2.2.

请注意,我并​​没有真正运行这个code,所以你必须要doublecheck错误。

Note, I didn't actually run this code, so you'll have to doublecheck for errors.