0

Code

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

this always return null even i gave it encoded64 (utf-8) string--->aGVsbG8=

Why this happening any one have idea..?? What i am doing Wrong can Any one Suggest me...

Robert
  • 39,162
  • 17
  • 99
  • 152
Drim
  • 514
  • 5
  • 18
  • 3
    Well you're returning null if an exception is thrown - you're calling `getMessage()` but not logging it or doing anything like that. My guess is that an exception *is* being thrown, but you have no idea what due to the lack of logging... – Jon Skeet Mar 13 '15 at 11:48
  • i know that but my question is why this happenig i have passed it 64base string even that so it not converting it into the bit map, – Drim Mar 13 '15 at 11:51
  • 1
    Hint: if you look at the exception, you'll find out why it's happening. That's the point of exceptions being richer than just "it failed". (If that's really your full base64 information, I suspect the problem is that it's not a complete image file.) – Jon Skeet Mar 13 '15 at 11:52
  • yeah i already heck it , at the my byte[] variable it return value but at a point of bitmap decoding it not retuning a bitmap to the bitmap object it empathy(null) <--- so why does it not decoding it into bitmap is my question , have u run this function ever? – Drim Mar 13 '15 at 11:57
  • I'm not sure how many times I can say this: *look at the exception. It will give you more information*. Or if there *isn't* an exception, that's information too. Once you've done that, post the exception details (or the lack of an exception) in your question. Where did you get the value from? It sounds like it's just not a valid image file - you should try taking something that *is* valid (e.g. a png file) and base64-encoding that. – Jon Skeet Mar 13 '15 at 11:58
  • please if any one have run this method so advice me and tell me does this function really work ? i have seen this method on 2 to 3 blog and they say it work but in my case why it not ..? have idea – Drim Mar 13 '15 at 12:00
  • I've already said what I suspect is wrong: your data isn't a valid image file. But as you refuse to tell us whether an exception is thrown or where you got the data from, it's *really* hard to help you. – Jon Skeet Mar 13 '15 at 12:01
  • jon u have to see code we have to pass a string<---- not a image u not see the proper my dear – Drim Mar 13 '15 at 12:02
  • can any one have code for converting string into the bitmap... that i want – Drim Mar 13 '15 at 12:03
  • Yes, you have to pass a string. But that string should be the base64-encoded representation of an image file. You can't just pass in any old base64 data and expect it to work. You're trying to create an *image*, so you've got to start with *image* data. Yet again, you haven't told us where you got this data from, or whether an exception is being thrown. Given that you seem intent on ignoring any advice and requests, I give up. – Jon Skeet Mar 13 '15 at 12:04
  • you can test your string with an online base64 image decoding tool e.g: http://www.askapache.com/online-tools/base64-image-converter/ to check it is correct – samgak Mar 13 '15 at 12:05
  • (http://practiceonandroid.blogspot.in/2013/03/convert-string-to-bitmap-and-bitmap-to.html) from here – Drim Mar 13 '15 at 12:05
  • i did i convert my string into 64base and also check online that does it decode same i convert the real one -->aGVsbG8= converted into 64bas and decode this in (https://www.base64encode.org/) here it perfect work – Drim Mar 13 '15 at 12:08
  • Did you encode the image using that site, or just the image file name? – samgak Mar 13 '15 at 12:12
  • it nottt a imageeee its a string which is encoded with64 bas check 2 my link – Drim Mar 13 '15 at 12:18
  • If it's not an image then how do you expect to decode it to a Bitmap? – samgak Mar 13 '15 at 12:18
  • please read the heading that in bold that its a string to bitmap not a image kindly don't make haste to answer me please refer a question first guys – Drim Mar 13 '15 at 12:20
  • 2
    A bitmap IS an image – samgak Mar 13 '15 at 12:20
  • Why don't you just take Jon's excellent advice and actually do something useful with the exception, like, say, log the message with Log.d("foobar", "exception", e); ? – JHH Mar 13 '15 at 12:52
  • 1
    `aGVsbG8=` is just the base64-encoded form of the ASCII encoding of "hello". What on earth did you *expect* that to return? If you expect it to be a Bitmap with that text drawn onto it, you're using *completely* the wrong approach. – Jon Skeet Mar 13 '15 at 13:01

2 Answers2

1

I think the problem is that you are trying to decode a base64 string to Bitmap, but actually you just want to decode it to a string. Here's the code to do that:

String decodeBase64String(String encodedString)
{
    byte[] data = Base64.decode(encodedString, Base64.DEFAULT);
    return new String(data, "UTF-8");
}

(assumes UTF-8 encoding)

If you call this function with your test string like this:

String result = decodeBase64String("aGVsbG8=");

then result will be "hello".

Here's how to convert text to a Bitmap:

Bitmap textToBitmap(String text)
{
     Paint paint = new Paint();
     paint.setColor(Color.WHITE);
     paint.setStrokeWidth(12);
     Rect bounds = new Rect();
     paint.getTextBounds(text, 0, text.length(), bounds);
     Bitmap bitmap = Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.ARGB_8888);
     Canvas canvas = new Canvas(bitmap);
     canvas.drawText(text, 0, 0, paint);
     return bitmap;
}

So you can convert your base64 encoded text to a bitmap like this:

String result = decodeBase64String("aGVsbG8=");
Bitmap bitmap = textToBitmap(result);

Or you could just do this:

Bitmap bitmap = textToBitmap("hello");
samgak
  • 23,944
  • 4
  • 60
  • 82
  • yeah and i try to converting it into the bitmap – Drim Mar 13 '15 at 12:29
  • Do you want to draw the text onto a Bitmap? – samgak Mar 13 '15 at 12:31
  • no i just want bit map of string that i write so i search for that and at end i got this function but it seems does not work, in short i want function to which i pass a string and it return me a bitmap of that string – Drim Mar 13 '15 at 12:37
  • Try the code in my answer. It will give you back your original string "hello". Why do you need a Bitmap? Bitmaps are for images not for text. – samgak Mar 13 '15 at 12:38
  • yeah thank you but i want bit map of that string like--> i pass hello so to in return i want a bitmap of hello – Drim Mar 13 '15 at 12:39
  • (http://practiceonandroid.blogspot.in/2013/03/convert-string-to-bitmap-and-bitmap-to.html) <------ see this site may u got what i not got it say u can convert string to bitmap – Drim Mar 13 '15 at 12:46
  • 1
    See my updated answer. I think you are confused about what base64 is for. It's not for drawing text onto Bitmaps. It's for encoding data (e.g. an image) as a text string and then converting the text back to the data. But try the code anyway. – samgak Mar 13 '15 at 12:50
1

you can revert your code using some other built in methods.

  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)
anupam sharma
  • 1,705
  • 17
  • 13