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nY/dj8/PyhGP0k/PzomP016Pz8/PysJWy8/Mj84HF14P0VZOTtFP0U/P2Q/TT91dD8/Xz8fP08/Pz8/Yz9PP3U/Q1Q/Pz8/P2g/Pww/Pyw/CEt8Pz8/P1c/JT8IPz8/Hz8ABj8/ET9+Mj8AP0M/Z0lkPz95P15qPz8/Xz8/P1I/Pw8/Oj91ID8bPz1HPz8/Pz9/P30/DTw/WT8/bwJoWns/fj99bXs/JGE/Nj9WLGFpCAoLPD8/ADc7ET8/Pz8aPz8/CT8/PgpaeB8/Pz8/ZRw/Pz8/Pz8/Pyo/P04/AFw/UAABVVVVQAAKP2w+Bws6FD8/Oj82P0s/Pz8APz8aPz9ybEZdPz8aPz9CPz8/bD8/Nj97dz9tfmhfBT8gPz8APz9RPz8QP2Y/DjQ/Hl9qPz8/dT8/Pz8/Pz8/HT94TT8/Pzo/U2w/P2Y/QSIWPz8kPz8/Pw9fPz0/Pz8/Pws/FhFbWT8nPx4aPz8GPz89Pz9PNE0qWj8uPz8NPz9zeD8pFD8/Pzc/Pz8TPwtNWj9LPz9LPzUbP1g/Pz8IdQc/UT9uP1E/ES4/aj9lSj8QPD8/ED8VPw0/AGB/BXgnPwU/PwA/Pz8/Pz9YGT8JLmU/PxY/RysxP3kKGT8tPD8/PzcEPz8/Zj8/Hz8VPz8Lbz8ePT8ealE/DT9pej0/Nj8lP2g/FisuPwwWP0IAEC8/PyQ/PzJeMT9UPz9dPz9fP08/Xz9NPz94Q00/Tz9NPj9LKj8PJz89Pz8yQj8nP1N3KD9yPzY/ZiM0UAc/";
byte[] b = s.getBytes();
Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
And the following to set gridview content:
imageView.setImageBitmap(bmp);
But all I'm getting is empty spots in my gridview! I wonder what is the problem?!
Try this,
byte[] decodedString;
decodedString = Base64.decode(s, Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
imgView.setImageBitmap(bitmap);
And let me know what happen...
String s is base64 string for image..
Related
I tried to convert base64 to bitmap, but bitmap value is returned as null. I tried a solution to remove data:image/png;base64.But base64 data does not contain that string.
Please suggest me a solution if anyone faced same issue.
byte[] decodedString = Base64.decode(base64, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
template.setImageBitmap(decodedByte);
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);
I have the following dataurl
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAARgAAADSCAMAAACmVC/YAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8wMi8xMi0xNzozMjowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L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2wvs5JfyN7Vyfrv9Nmgl2RDHIEKOpCAhAMmj5+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);
I have created a base64 string from a picture on the SD card using this(below) code, and it works but when I try to decode it (even further below) I get a java.lang.outOfMemoryException, presumably because I am not splitting the string into a reasonable size before I decode it as I am before I encode it.
byte fileContent[] = new byte[3000];
StringBuilder b = new StringBuilder();
try{
FileInputStream fin = new FileInputStream(sel);
while(fin.read(fileContent) >= 0) {
b.append(Base64.encodeToString(fileContent, Base64.DEFAULT));
}
}catch(IOException e){
}
The above code works well, but the problem comes when I try to decode the image with the following code;
byte[] imageAsBytes = Base64.decode(img.getBytes(), Base64.DEFAULT);
ImageView image = (ImageView)this.findViewById(R.id.ImageView);
image.setImageBitmap(
BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
);
I have tried this way too
byte[] b = Base64.decode(img, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
image.setImageBitmap(bitmap);
Now I assume that I need to split the string up into sections like my image encoding code, but I have no clue how to go about doing it.
You need decode the image in Background thread like AsyncTask
or
you need reduce your image quality using BitmapFactory .
Example:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
options.inPurgeable=true;
Bitmap bm = BitmapFactory.decodeFile("Your image exact loaction",options);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
You have two problem
base64 encoding requires much more space. 3 bytes convert to 4 chars (Factor 8/3)
you read the whole file at once
in same way your first approach solved this issues. So just use that version
By that way, why are you using decodeByteArray and not decodeFile
You might try to decode to a temporary file and create image from that file.
As to base64, it is 6 bits per character, or 6x4=24 bits=3 bytes per 4 characters.
So if you take 4 characters of base64, you will not break the corresponding 3 bytes.
That is, you may split the base64-encoded data at character indices that are a multiple of 4.
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