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...

20
  • 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... Commented Mar 13, 2015 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, Commented Mar 13, 2015 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.) Commented Mar 13, 2015 at 11:52
  • 2
    A bitmap IS an image Commented Mar 13, 2015 at 12:20
  • 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. Commented Mar 13, 2015 at 13:01

2 Answers 2

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");
Sign up to request clarification or add additional context in comments.

7 Comments

yeah and i try to converting it into the bitmap
Do you want to draw the text onto a Bitmap?
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
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.
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.
|
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)

1 Comment

i did same for that in function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.