How to set Decoded bytes to Imageview - android

I receive byte string in response but whenever i trying to convert it in bitmap it giving me null value.I am using below code for converting to bitmap.Anyone Help me?
byte[] decodedString = Base64.decode(strImageid, Base64.NO_WRAP);
Bitmap decodedByte = BitmapFactory.decodeByteArray( decodedString, 0, decodedString.length);
holder.imageView.setImageBitmap(decodedByte);

it's working fine using glide getting bitmap images.
private void applyProfilePicture(EmployeeViewHolder holder, List<AllRestaurantList> dataList, final int position) {
if (!TextUtils.isEmpty(dataList.get(position).getImage())) {
String imageBytes = dataList.get(position).getImage();
byte[] imageByteArray = Base64.decode(imageBytes, Base64.DEFAULT);
Glide.with(context)
.load(imageByteArray)
.asBitmap()
.into(holder.ivResIcon);
/*Glide.with(context)
.load(dataList.get(position).getImage())
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(holder.ivResIcon);*/
// holder.img.setColorFilter(null);
//holder.iconText.setVisibility(View.GONE);
} else {
holder.ivResIcon.setImageResource(R.drawable.ic_perm_identity_gray_24dp);
//holder.imgProfile.setColorFilter(dataList.getColor());
// holder.iconText.setVisibility(View.VISIBLE);
}
}

As mentioned in the comment's link If you have converted your image to string by using Base64. You should convert it back with Base64.
Im using this snippet maybe it helps u too.
byte[] data = Base64.decode(iconString, Base64.DEFAULT);
Bitmap imageBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, null); // null for options
imageView.setImageBitmap(imageBitmap);

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);

Get Blob image and convert that image into Bitmap image

I am getting image from database in blob format. i want to convert it into Bitmap image.the code i used to convert bitmap to Blob is put below.but please tell me how to reverse it.???
ByteArrayOutputStream boas = new ByteArrayOutputStream();
btmap.compress(Bitmap.CompressFormat.JPEG, 100, boas ); //bm is the bitmap object
byte[] byteArrayImage = boas .toByteArray();
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
This will work
byte[] byteArray = DBcursor.getBlob(columnIndex);
Bitmap bm = BitmapFactory.decodeByteArray(byteArray, 0 ,byteArray.length);
You can use this simple static function, It is super easy to implement and reuse :)
public static Bitmap getBitmapFromBytes(byte[] bytes) {
if (bytes != null) {
return BitmapFactory.decodeByteArray(bytes, 0 ,bytes.length);
}
return null;
}

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;
}

How to use base64 encoded jpeg in Android gridview?

I'm doing for testing purposes.
I have this image:
I encoded it to base 64 using this app and the following is the result:
Pz8/PwAQSkZJRgABAQEAYABgAAA/Pw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08/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..

Categories

Resources