Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android convert base64 encoded string into image view

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));
like image 986
Baskar Avatar asked Sep 06 '25 13:09

Baskar


1 Answers

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);
like image 111
Husam A. Al-ahmadi Avatar answered Sep 09 '25 05:09

Husam A. Al-ahmadi