Error in Base64 string To image - android

android code Error :
byte[] decodedString = Base64.decode(""aHR0cHM6Ly9ldGlja2V0LmlwZWt0ci5jb20vd3Nib3MzL0xvZ29WZXIuQXNweD9mbnVtPTI2NQ=="", Base64.DEFAULT);
Bitmap base64Bitmap = BitmapFactory.decodeByteArray(decodedString, 0,
decodedString.length);
Log.d("img", String.valueOf(base64Bitmap));
imagview.setImageBitmap(base64Bitmap);
logcat Message
SkImageDecoder::Factory returned null

Your base64 string is corrupted.
Check it via below link:
http://codebeautify.org/base64-to-image-converter
Please try to decode some different string and then check it.
or try below code :
byte[] encodeByte = Base64.decode("aHR0cHM6Ly9ldGlja2V0LmlwZWt0ci5jb20vd3Nib3MzL0xvZ29WZXIuQXNweD9mbnVtPTI2NQ", Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0,
encodeByte.length);
return bitmap;
If still it won't work try Base64.NOWRAP instead of Base64.DEFAULT.

Try this
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).build();
ImageLoader.getInstance().init(config);
ImageLoader imageLoader = ImageLoader.getInstance();
ImageView imageView = (ImageView) this.findViewById(R.id.imageView);
try {
url = decodeBase64String(base64String);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
imageLoader.displayImage(url, imageView);
String decodeBase64String(String encodedString) throws UnsupportedEncodingException {
byte[] data = Base64.decode(encodedString, Base64.DEFAULT);
return new String(data, "UTF-8");
}
set dependency -compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'

Check this function:
public Bitmap StringToBitMap(String encodedString) {
try {
byte[] encodeByte = Base64.decode(encodedString, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0,
encodeByte.length);
return bitmap;
} catch (Exception e) {
e.getMessage();
return null;
}
}

Related

Camera base64 image cannot be converted to Bitmap

I have been having trouble converting the base64 String Image that is being sent to me by the backend.
So this is how the backend sends the data.
final ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
ImageIO.write(img, formatName, Base64.getEncoder().wrap(os));
return os.toString(StandardCharsets.ISO_8859_1.name());
} catch (final IOException ioe) {
throw new UncheckedIOException(ioe);
}
And this is how we convert it.
final byte[] data = Base64.decode(base64, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(data, 0, data.length);
The result is always null. Any help is highly appreciated.
EDIT:
These are the back end codes.
#Override
public BufferedImage base64ToImage(String base64Image) throws IOException {
byte[] imageBytes = Base64.getDecoder().decode(base64Image);
BufferedImage img = ImageIO.read(new ByteArrayInputStream(imageBytes));
return img;
}
public static String imgToBase64String(final RenderedImage img, final String formatName) {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
ImageIO.write(img, formatName, Base64.getEncoder().wrap(os));
return os.toString();
} catch (final IOException ioe) {
throw new UncheckedIOException(ioe);
}
}
And this is our converting tool before sending the Base64 Image String to the back end.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return Base64.encodeToString(baos.toByteArray(), Base64.NO_WRAP);
Check that ur base64 encoded string does not start with data:image/jpg;base64,. If it does then remove it. The Base64.decode won't be able to decode it in this case. u can remove it by using encodedString.substring(encodedString.indexOf(",") + 1);. Let me know if this solves ur problem.
Try this,
ImageView driverImage = (ImageView) driverReportView.findViewById(R.id.imgViewDriverImage);
try {
byte [] encodeByte = Base64.decode(driverImageString.replace("\'/", "/"), Base64.DEFAULT);
Bitmap bitmap= BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
if (bitmap != null)
driverImage.setImageBitmap(bitmap);
}catch (Exception ex){
Log.e(TAG,"Error to display driver info "+ex.toString());
}
Where
driverImageString is your image string

Issue in Covert from String Base64 to Bitmap

Code stuff for convert from Bitmap to String Base64
Bitmap thumbnail = extras.getParcelable("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, baos);
thumbnail.recycle();
byte[] b = baos.toByteArray();
String attachment = Base64.encodeToString(b, Base64.DEFAULT);
Code stuff for convert from String Base64 to Bitmap
byte[] encodeByte = Base64.decode(strBase64, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
imageView.setImageBitmap(bitmap);
but i get bitmap = null;
I also refer Base64 to Bitmap to display in ImageView
Thanks in advance.
// convert Bitmap to String
public static String BitMapToString(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] arr = baos.toByteArray();
imageData = Base64.encodeToString(arr, Base64.DEFAULT);
return imageData;
}
// Convert String to Bitmap
public static Bitmap StringToBitMap(String image) {
try {
byte[] encodeByte = Base64.decode(image, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0,
encodeByte.length);
return bitmap;
} catch (Exception e) {
e.getMessage();
return null;
}
}
You are calling thumbnail.recycle(); after loading bitmap,, either remove it or call before loading bitmap

Convert String to Bitmap

In my project i will retrieve image from mysql database and display it to Imageview. In database I have saved the link of image. So I need to convert String to Bitmap to display image. But i got error like setImageBitmap is undefined for the type of String. I am not sure what mistake i have done.
Code:
Bitmap b=StringToBitMap(Qrimage);
imgg.setImageBitmap(b);
public Bitmap StringToBitMap(String image){
try{
byte [] encodeByte=Base64.decode(image,Base64.DEFAULT);
InputStream inputStream = new ByteArrayInputStream(encodeByte);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}catch(Exception e){
e.getMessage();
return null;
}
}
change your function
public Bitmap StringToBitMap(String encodedString){
try{
byte [] encodeByte = Base64.decode(encodedString,Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
}
catch(Exception e){
e.getMessage();
return null;
}
}
// second solution is you can set the path inside decodeFile function
viewImage.setImageBitmap(BitmapFactory.decodeFile("your iamge path"));
hopefully it will work for you
If you get bitmap = null, you can use:
byte[] decodedString = Base64.decode(pic, Base64.URL_SAFE );
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

how we can encode and decode the image string data and show on imageview

i am getting the decoded string data of image and i want to encode that data and show on image view. i getting the null value in bitmap.
that is what i have done so far.
#SuppressLint("NewApi")
public void show(View view){
byte[] imageBytes = null;
try {
imageBytes = imagedata.getBytes("UTF-8");
break;
// here you could place handling of other clicks if necessary...
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream in = new ByteArrayInputStream(imageBytes);
Bitmap b = BitmapFactory.decodeStream(in);
image.setImageBitmap(b);
}
to decode an image use the following code:
byte[] decodedString = Base64.decode(image_path, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length, options);
// options.inJustDecodeBounds = false;
promoIV.setImageBitmap(decodedByte);

android decode string base 64 to bitmap

Hi guys I wanted to ask you one thing, I have a chat that transfers strings and I can even attach of JPEG images before sending them to convert it into a string and then decode in BITMAP just that when I decode it crashes the app. I wanted to know if it is the right code to decode it.
NOME = (TextView) row.findViewById(R.id.comment);
NOME.setText(coment.comment);
String a = NOME.getText().toString();
if(a.length() > 1024 )
{
byte[] image = Base64.decode(a, 0);
int lung = a.length();
Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, lung);
Image = (ImageView) row.findViewById(R.id.image);
Image.setImageBitmap(bitmap);
}
The code looks fine, if I had to guess I would say you're getting the Out of Memory error, which is very common when loading images. Check out
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
for some best practices when loading images.
The method for Encoding an Image to String Base64 :
public static String encodeToString() {
String imageString = null;
try {
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
imageString = Base64.encodeToString(b, Base64.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
}
return imageString;
}
The method for Decoding String Base64 to Image :
public static void decodeToImage(String imageString) {
try {
byte[] imageByte = Base64.decode(imageString, Base64.DEFAULT);
Bitmap bm = BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length);
image_view.setImageBitmap(bm);
} catch (Exception e) {
e.printStackTrace();
}
}

Categories

Resources