Android convert base64 encoded string into image view

限于喜欢 提交于 2019-12-09 04:18:10

问题


I want to convert base64 encoded string into bitmap so i can put it in image view, but getting error like

D/skia(7490): --- decoder->decode returned false and bitmap returns null value

My code is:

byte[] imageAsBytes = Base64.decode(imageData);

image.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));

回答1:


Firts you have to check that the string you want to decode is vaild and has the intended value to be decoded and to do so, you can do something like below:

filePath= Environment.getExternalStorageDirectory()
                        + "/SaudiScore/temporary_holder.jpg";
Bitmap selectedImage =  BitmapFactory.decodeFile(filePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
selectedImage.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
String strBase64=Base64.encodeToString(byteArray, 0);

then you can decode the string that you just encoded and get the image back by doing something like the following:

byte[] decodedString = Base64.decode(strBase64, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
image.setImageBitmap(decodedByte);



回答2:


byte[] decodedString = Base64.decode(mBase64string, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,decodedString.length);
mImageView.setImageBitmap(decodedByte);



回答3:


String base = "Base64 string values of some image";

byte[] imageAsBytes = Base64.decode(base.getBytes(), Base64.DEFAULT);

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

image.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));

Try this code.




回答4:


This method can help:

private void setExistImage(ImageView imageView, String base64String){
    if (!base64String.isEmpty()) {
        byte[] bytes = Base64.decode(base64String, Base64.DEFAULT);
        imageView.setImageBitmap(BitmapFactory.decodeByteArray(bytes, 0, bytes.length));
    }
}



回答5:


Decode/Convert base64 string to image

    imageBytes = Base64.decode(imageString, Base64.DEFAULT);
    Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0,imageBytes.length);
    image.setImageBitmap(decodedImage);



回答6:


 byte[] decodedString = Base64.decode(strBase64, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
image.setImageBitmap(decodedByte);

I was using the above solution .It always returned errors like decoded String is null,IllegalStateException .. All I did was I just wrapped that in a try catch



来源:https://stackoverflow.com/questions/15683032/android-convert-base64-encoded-string-into-image-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!