Bitmap decodedByte = BitmapFactory.decodeByteArray always null - android

While trying to decode base64 string and convert it back to an image
Bitmap decodedByte = BitmapFactory.decodeByteArray
always returns null.here i have mentioned what i am trying.
String base64="9j/4AAQSkZJRgABAQAAAQABAAD/4gIoSUNDX1BST0ZJTEUAAQEAAAIYAAAAAAIQAABtbnRyUkdC
IFhZWiAAAAAAAAAAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQA.....it goes lenthy";
byte[] imageAsBytes = Base64.decode(base64.getBytes(), Base64.DEFAULT);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = false;
Bitmap decodedByte = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length, opts);
cancelceque_img.setImageBitmap(decodedByte);
this method i have used to convert base64 to image,any one give me the solution.

Related

facing problem while convert byte array to bitmap in android

my code
String passenger_sign = assignedJobJson.getJSONObject(position).getString("passenger_sign");
Log.e(TAG, "passenger_sign: "+passenger_sign );
byte[] Bytedata = passenger_sign.getBytes();
Log.e(TAG, "Bytedata: "+Bytedata );
ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(Bytedata);
Bitmap bitmap = BitmapFactory.decodeStream(arrayInputStream);
//Bitmap bmp = BitmapFactory.decodeByteArray(Bytedata, 0, Bytedata.length);
Log.e(TAG, "bitmap: "+bitmap );
problem
i want to convert byte array to bitmap but it showing only null value while converting to bitmap.
Assuming your passenger_sign contains the image data converted in Base64, you can apply the following code:
byte[] decodedString = Base64.decode(passenger_sign, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
// Now, we set the decoded bytes to an image view to check if conversion is successfull
imageView.setImageBitmap(decodedByte);

How to load an image from dataurl in android imageview?

I have the following dataurl
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAARgAAADSCAMAAACmVC/YAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpV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M8fJ9YbhPn0ybzFTK+vZ/ZxjSR0Wk4L1UU5hpep6g86xetNuuira4Iky6vo9aSrS9WXDXcuasIQ63RG23c60mTwo3Hy01hHvZqE1myTvIS/yXXuC6DYRWivMTstbEzOSV9LQW2rqcl0Hc6yd/U2rjvF/gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH7IdixtjXannItg1w8WxhBniq59rIr/I3OmwiLGHraC/hW/a2s/n+ixg419b8AHyCGES4X6WNZom+2anff7uZwxooP4EQq6XK7irn8aqDIE+yInN3Z58t+v8cdEd7J/7X2wvs5JfyN7Vyfrv9Nmgl2RDHIEKOpCAhAMmj5+KJBGjCC26IPoIhCMBqiKhPFjRHI4BmwmMRgNmyENRiwMNkMZzHadkgGbCQzmSwyfz0PewRvMMx2+jshUJT2YY+jokHqIDcalQn14sspk7ZAaYTPBECmwlA02c9WbKAcjYTPzSJ2z6pPjn2sz00GS5F3O5+jMAAAAAAAAAAAAAAAAAAAAAADAL/n7nOs09bYCYvG3+WlXLNtCzWNnf0HspptVEEu08HuUhcCZy7tyeHRNnuhkhfSW3etH3vF4rP6ghFHJJW/JPeuNMhMX21ErX8q3exMXRXawqIi+KzJThu/FDW4LdZNk8/u9/OWXYmds1qWNj8aWd/75qxC1
is there a way to load the dataUrl in imageview?
First you need to convert the Base64 encoded string to a Bitmap.
Code snippet below from linked question:
byte[] decodedString = Base64.decode("Your Base64 String", Base64.DEFAULT);
Bitmap bitMap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
When that is done you have your bitmap in bitmap
Assuming your imageViewis defined as R.id.image_view
Then just do as you would with any other bitmap:
ImageView imageView;
imageView = (ImageView) findViewById(R.id.image_view);
imageView.setImageBitmap(bitmap);
Since I was getting the error:
java.lang.IllegalArgumentException: bad base-64
, I used the following workaround:
import java.io.ByteArrayInputStream
import java.io.InputStream
import android.util.Base64
...
val imageData: ByteArray = Base64.decode(dataUri.substring(dataUri.indexOf(",") + 1), Base64.DEFAULT)
val inputStream: InputStream = ByteArrayInputStream(imageData)
val bitmap: Bitmap = BitmapFactory.decodeStream(inputStream)
imageViewComponent.setImageBitmap(bitmap)
Perhaps it will help someone.
Just decode the Base64 image encoding using Base64.decode() method to receive a byte array, and then create the Bitmap using BitmapFactory.decodeByteArray() method.
If you take the Base64 part of the input, it can easily be converted to a Bitmap via this:
byte[] decodedString = Base64.decode("your base64 string", Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
get base64 string in String variable decode
String encodedImage;// set base64 string value in this string
byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

How to convert Bitmap to string without losing quality

I've converted an bitmap image into string to save it:
............
Bitmap photo = extras.getParcelable("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
Then I retrieve the bitmap from string to set an activity's background just like that:
byte[] temp = Base64.decode(encodedImage, Base64.DEFAULT);
Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0,
temp.length, options);
Drawable d = new BitmapDrawable(getResources(), bitmap);
getWindow().setBackgroundDrawable(d);
Everything works fine but the image quality reduces tremendously. I know it is because photo.compress(Bitmap.CompressFormat.JPEG, 100, baos); as I am compressing this image(though in best compressing resolution). So how can I do this without compressing the image? I have also tried the following code but it returns nothing
.......
Bitmap photo = extras.getParcelable("data");
int bytes = photo.getWidth() * photo.getHeight() * 4;
ByteBuffer buffer = ByteBuffer.allocate(bytes);
photo.copyPixelsToBuffer(buffer);
byte[] b = buffer.array();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
Also I am using sharedPreference to save the image. I thought of sqlite or internal storage also, but in both case compressing is needed as I found in internet

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));
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);
byte[] decodedString = Base64.decode(mBase64string, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,decodedString.length);
mImageView.setImageBitmap(decodedByte);
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.
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));
}
}
Decode/Convert base64 string to image
imageBytes = Base64.decode(imageString, Base64.DEFAULT);
Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0,imageBytes.length);
image.setImageBitmap(decodedImage);
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

How to convert base64Binary string to image in Android?

i tried this code
byte decodedIcon[] = null;
byte[] bb = (resposeString).getBytes("utf-8");
decodedIcon = Base64.decodeBase64(bb);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedIcon, 0,decodedIcon.length);
I included commons-codec-1.3.jar
Still it returns null, any help will be greatly appreciated.
I use Following function for convert Base64 String to Image.
public Bitmap Base64ToImage(String base64String) {
byte[] imageAsBytes = Base64.decode(base64String.getBytes(),
Base64.DEFAULT);
Bitmap mybitmap = BitmapFactory.decodeByteArray(imageAsBytes, 0,
imageAsBytes.length);
return mybitmap;
}

Categories

Resources