byte[] won't parse to an image - java

I have this custom adapter for my listview that is getting filled with a ArrayList of a custom rowItem class where I put data in.
rowItem.getImage() will return this byte array:
I have put the long string under this in a byte[] variable with getBytes()
R0lGODlhZAAvAPcAAAAAAAAAMwAAZgAAmQAAzAAA/wArAAArMwArZgArmQArzAAr/wBVAABVMwBVZgBVmQBVzABV/wCAAACAMwCAZgCAmQCAzACA/wCqAACqMwCqZgCqmQCqzACq/wDVAADVMwDVZgDVmQDVzADV/wD/AAD/MwD/ZgD/mQD/zAD//zMAADMAMzMAZjMAmTMAzDMA/zMrADMrMzMrZjMrmTMrzDMr/zNVADNVMzNVZjNVmTNVzDNV/zOAADOAMzOAZjOAmTOAzDOA/zOqADOqMzOqZjOqmTOqzDOq/zPVADPVMzPVZjPVmTPVzDPV/zP/ADP/MzP/ZjP/mTP/zDP//2YAAGYAM2YAZmYAmWYAzGYA/2YrAGYrM2YrZmYrmWYrzGYr/2ZVAGZVM2ZVZmZVmWZVzGZV/2aAAGaAM2aAZmaAmWaAzGaA/2aqAGaqM2aqZmaqmWaqzGaq/2bVAGbVM2bVZmbVmWbVzGbV/2b/AGb/M2b/Zmb/mWb/zGb//5kAAJkAM5kAZpkAmZkAzJkA/5krAJkrM5krZpkrmZkrzJkr/5lVAJlVM5lVZplVmZlVzJlV/5mAAJmAM5mAZpmAmZmAzJmA/5mqAJmqM5mqZpmqmZmqzJmq/5nVAJnVM5nVZpnVmZnVzJnV/5n/AJn/M5n/Zpn/mZn/zJn//8wAAMwAM8wAZswAmcwAzMwA/8wrAMwrM8wrZswrmcwrzMwr/8xVAMxVM8xVZsxVmcxVzMxV/8yAAMyAM8yAZsyAmcyAzMyA/8yqAMyqM8yqZsyqmcyqzMyq/8zVAMzVM8zVZszVmczVzMzV/8z/AMz/M8z/Zsz/mcz/zMz///8AAP8AM/8AZv8Amf8AzP8A//8rAP8rM/8rZv8rmf8rzP8r//9VAP9VM/9VZv9Vmf9VzP9V//+AAP+AM/+AZv+Amf+AzP+A//+qAP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
Now I have this code:
byte[] imageData = rowItem.getImage();
Drawable logoDrawable = null;
if (imageData != null) {
Bitmap logoBitmap = BitmapFactory.decodeByteArray(imageData, 0,
imageData.length);
logoDrawable = new BitmapDrawable(context.getResources(), logoBitmap);
}
holder.imageView.setImageDrawable(logoDrawable);
But it won't display a image, if I set a image out of my drawable map it will show so it reconizes the imageview.

What you've shown isn't a byte array - it's text, which looks very much like base64 data.
You should try decoding it from base64 to "raw" bytes first:
byte[] imageData = Base64.decode(rowItem.getImage(), Base64.DEFAULT);
... or fix wherever you're getting rowItem from to perform the base64 decoding for you.

The data looks like base64 encoded binary data. Base64.decode() it first and then pass the decoded data to BitmapFactory.

Related

Trying to display image retrived from server with base64 encoding but cant display the correct image

These are my encoding and decoding methods.
public String encodeBase64(Bitmap bit){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bit.compress(Bitmap.CompressFormat.PNG, 100, baos); // Could be Bitmap.CompressFormat.PNG or Bitmap.CompressFormat.WEBP
byte[] bai = baos.toByteArray();
return Base64.encodeToString(bai, Base64.DEFAULT);
}
public Bitmap decodeBase64(String base64Image){
byte[] data = Base64.decode(base64Image, Base64.DEFAULT);
Bitmap bm;
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inMutable = true;
bm = BitmapFactory.decodeByteArray(data, 0, data.length, opt);
return bm;
}
This is encoded data from a bitmap
iVBORw0KGgoAAAANSUhEUgAABLAAAAdcCAYAAAC7T3KrAAAABHNCSVQICAgIfAhkiAAAIABJREFU
eJzswQEBAAAAgJD+r+4ICgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.... etc
But the data i get from the server looks like this
{"type":"Buffer","data":[105,86,66,79,82,119,48,75,71,103,111,65,65,65,... etc
I can get pure data or the type and data json object. Ive tried to decode both. When i try to decode with the type and data it gives me a null bitmap, when i decode the raw data only the image is not displayed, but nothing seems to crash.
Do i need to retrive all those array numbers and convert them to chars before decoding? Ive read other post and it sounded very straigtforward, just encode and then decode.
decode base_64 string Imgae in codigneter
$
aadharcard_front = image name(database)
if(!empty($aadharcard_front))
{
$aadharcard_front = base64_decode($aadharcard_front);
$aadharcard_front_time = time().'.png';
file_put_contents(APPPATH.'../media/uploads/'.$aadharcard_front_time,$aadharcard_front);
$user_data['aadharcard_front'] = $aadharcard_front_time;
}

Android Image to Base64

I want to convert my app images to base64 so it won't show on the gallery. I have tried various techniques. The image is from a zip file so it is a byte array at that point. The length of the base64 change when I change the byte array size. What is the proper byte array size? And the base64 encoded image doesn't work too.
The primary code is
String encodedImage = Base64.encodeToString(buffer, Base64.DEFAULT);
buffer is the byte array(102400)
and it contains the image too.
The image is a 7KB file and the output is 400KB
You may try following function to convert image into Base64:
public void toStringImage(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
}
Above function takes Bitmap image and converts it into Base64 encoded string. This is working in my project and I hope this will help you too.

Why can't I decode Android encoding image using base64String with an other decoder?

I have an image that I encode using this code :
Bitmap bm = BitmapFactory.decodeFile(selectedImagePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
It encodes well. After that I'm getting the String in a file in my computer and I try to decode it with this website :
http://www.askapache.com/online-tools/base64-image-converter/
And I'm getting a null picture. However, when I decode it directly in my app and display it like :
public static Bitmap decodeBase64(String input) {
byte[] decodedByte = Base64.decode(input, 0);
return BitmapFactory
.decodeByteArray(decodedByte, 0, decodedByte.length);
}
private ImageView img;
img.setImageBitmap(decodeBase64(encodedImage));
I'm getting the right picture. So I was wondering if i actually can't decode an android encoded image with an other decoder than android's one or my file is corrupted meaning that I have missing characters. However even if I don't have the whole encoded file, the decoded picture i'm getting with the website should look like to the good one right ?
Thank you in advance

Java Convert String to byte[]

I wonder how you should do if you are going to convert an String that contains an array of bytes, actually bytes from an image into an byte array.
I guess this is a pretty common issue so i guess someone could give any suggestions.
If it is better to convert an JSONArray into an byte array i would like to know why.
I wonder how you should do if you are going to convert an String that contains an array of bytes, actually bytes from an image into an byte array.
It depends on how you've constructed the string in the first place. If something somewhere has used
new String(imageData)
then you've already lost information, potentially. Image data isn't text.
To propagate opaque binary data in text, you should use base64 (or potentially hex). Android already has a Base64 class that makes this easy:
String text = Base64.encodeToString(originalBytes, Base64.DEFAULT);
...
byte[] roundTrip = Base64.decode(text, Base64.DEFAULT);
EDIT: If you can't use that class, this public domain base64 encoder should work fine.
Byte[] to String
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Your_Bitmap.compress(CompressFormat.PNG, 0 , baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
String bytetostring = Base64.encodeToString(b, Base64.DEFAULT);
return bytetostring ;
String to Byte[]
byte[] data = Base64.decode("Your string", Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data.length);

How to convert image to byte array and convert back byte array to image

I am trying to convert an image to byte array and converting back byte array to image in Android Emulator.
First part is working fine but the second part is not creating the image file in Android emulator.
Please suggest me if there is any correction in my second part of the code.
Following is my code.
public String GetQRCode() throws FileNotFoundException, IOException {
/*
* In this function the first part shows how to convert an image file to
* byte array. The second part of the code shows how to change byte array
* back to an image.
*/
Bitmap bitmap = BitmapFactory.decodeFile("sdcard/Download/QR.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 60, baos);
byte[] byte_img_data = baos.toByteArray();
byte[] buf = new byte[200];
// Second Part: Convert byte array back to an image
Bitmap bitmap2 = BitmapFactory.decodeByteArray(byte_img_data, 0, 200);
ByteArrayOutputStream img= new ByteArrayOutputStream();
Bitmap imageFile= BitmapFactory.decodeFile("sdcard/Download/QR3.jpg");
String abc = buf.toString();
return abc;
}
your call to BitmapFactory.decodeByteArray(..) - this method returns a Bitmap object, but you're not assigning it to anything. You also need to change the call to pass in the actual length of byte_img_data, rather then 200.
Bitmap bitmap2 = BitmapFactory.decodeByteArray(byte_img_data, 0, byte_img_data.length);
However, whether decodeByteArray(..) can handle compressed streams, i don't know

Categories

Resources