在Android中解码base64字符串到位图

时间:2022-11-03 18:34:14

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=

即使我给它赋了encoded64 (utf-8)字符串-->aGVsbG8=,它也总是返回null

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

为什么会发生这种事?我做错了什么,谁能告诉我……

4 个解决方案

#1


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:

我认为问题是你试图把一个base64字符串解码成位图,但实际上你只是想把它解码成一个字符串。下面是这样做的代码:

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

(assumes UTF-8 encoding)

(假定utf - 8编码)

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:

所以你可以把你的base64编码的文本转换成这样的位图:

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

Or you could just do this:

或者你可以这样做:

Bitmap bitmap = textToBitmap("hello");

#2


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)

#3


0  

Try this, might help you..

试试这个,可能对你有帮助。

String base="****Your Base64 string******”;

字符串基地= " * * * *你Base64字符串* * * * * *”;

byte[] imageBytes = Base64.decode(base.getBytes(), Base64.DEFAULT); ImageView.setImageBitmap(BitmapFactory.decodeByteArray(imageBytes, 0,imageBytes.length)

字节[]imageBytes = Base64.decode(base.getBytes(), Base64.DEFAULT);ImageView.setImageBitmap(BitmapFactory.decodeByteArray(imageBytes 0 imageBytes.length)

#4


0  

i checked it. it's working fine.

我检查它。这是工作正常。

may be your passing wrong "encodedString".

可能是您传递错误的“encodedString”。

test the below code

测试以下代码

String strBase64= "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAFA3PEY8MlBGQUZaVVBfeMiCeG5uePWvuZHI////////////////////////////////////////////////////2wBDAVVaWnhpeOuCguv/////////////////////////////////////////////////////////////////////////wAARCABeAJoDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0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Jo3UbqBhiijdSZoAWm0uaKAP/2Q==";

字符串strBase64 =“/ 9 j / 4 aaqskzjrgabaqaaaqabaad / 2 wbdafa3pey8mlbgquzavvbfemiceg5uepwvuzhi / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / 2 wbdavvawnhpeoucguv / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / wAARCABeAJoDASIAAhEBAxEB / 8 qahwaaaqubaqebaqeaaaaaaaaaaaecawqfbgcicqol / 8 qatraaagedawieawufbaqaaaf9aqidaaqrbrihmuege1fhbyjxfdkbkaeii0kxwrvs0fakm2jyggkkfhcygroljicokso0nty3odk6q0rfrkdisuptvfvwv1hzwmnkzwznaglqc3r1dnd4exqdhiwgh4ijipktljwwl5izmqkjpkwmp6ipqrkztlw2t7i5usldxmxgx8jjytlt1nxw19jz2uh当+ Tl5ufo6erx8vP09fb3 + Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL / 8 qatreaagecbaqdbacfbaqaaqj3aaecaxeebsexbhjbuqdhcrmimoeifekrobhbcsmzuvavynlrchyknoel8rcygromjygpkju2nzg5okneruzhselku1rvvldywvpjzgvmz2hpann0dxz3ehl6gooehyahiimkkpoulzaxmjmaoqokpaanqkmqsro0tba3ulm6wspexcbhymnk0tpu1dbx2nna4upk5ebn6onq8vp09fb3 + Pn6/9oADAMBAAIRAxEAPwCsqgrnFLsHpQn3RTqoY3YPSjYPSnUUAN2L6UbF9KfRQAzYvpRsX0p9FADNi + lGxfSn0UAM2L6UbB6U / FJQIbsHpRsHpTqKAG7R6Um0elPpKYDdg9Kd5YpVqbhEU7QSfWiyIbK5THao6suBk4BA9DVWlJDi7kyfcFOpqH5BS7vagsWik3UbqAFpaTNLmgAopNwo3CgBaQ5A460m + lQlnAoewAM9 + tFDkhyOKbuPtQtgHUUmaOaYC0lJ83rRz60CHDinrIyjAIx7iosmjJoE1cezZySck1WqbJqGlIErD1 + 7 s0l90uufbsu6jfawahfgamumkasjrs4ooaspifv / AIUypYPvE + 1 j7anlhz1hus33h9kaka2abztlnlg9qagphyummyjqtaanppbyjcmny9axbs4ocw3fqvzxvakxei4x1pwxptmohtgspykvxpknlt6ubg9kesyec8gpqaertuxoqhgpajy3pu + KMUXAh8qjyanoouBX8k0oAjVgTyanqGSMs / Hf3pMTEKhwuDnAo8oUsaFX5 nUuKECIdnvQFI71Nim4p3HYbg0Yp + KTFAhmKMU / FJigBlVqsuwUc1WoZLJYdufmp8yoeU49qgU4pSxPGaRFtRKnhnCrtYE1BUsRCg5HPrQVexI1yv8IP41JE + 9 mkjpeqbdaccKqsrHd3pD5i7SVXFw / dc09Zlbvg + 9 luu6jgiuc0wou4k8yfjnygp5veic9wd70c6kxdqt705sggrvfyvqg / SpYxtTIPFAIkpKjaUKM5z + NM88 / wB2mO6J6aWCjkiq6uzuAWIzTW + 8 cnpvtj5h7totxwkjyt3pzmcaabxtacb3dvztadsuaapaqhfluoakatjfg6jd7ualg0nwjdru9qbcjiowatdruoadkjhtt9ynlsk1fuo3uhkm4aflkum44i9azuo3uakcmg5oktdrupiazfhjo3ubqbhiijdszoawm0uakap / 2 q = =”;

    Log.d("base64 String: ", strBase64);


    byte[] decodedString = Base64.decode(strBase64, Base64.DEFAULT);
    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
    test.setImageBitmap(decodedByte);

#1


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:

我认为问题是你试图把一个base64字符串解码成位图,但实际上你只是想把它解码成一个字符串。下面是这样做的代码:

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

(assumes UTF-8 encoding)

(假定utf - 8编码)

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:

所以你可以把你的base64编码的文本转换成这样的位图:

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

Or you could just do this:

或者你可以这样做:

Bitmap bitmap = textToBitmap("hello");

#2


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)

#3


0  

Try this, might help you..

试试这个,可能对你有帮助。

String base="****Your Base64 string******”;

字符串基地= " * * * *你Base64字符串* * * * * *”;

byte[] imageBytes = Base64.decode(base.getBytes(), Base64.DEFAULT); ImageView.setImageBitmap(BitmapFactory.decodeByteArray(imageBytes, 0,imageBytes.length)

字节[]imageBytes = Base64.decode(base.getBytes(), Base64.DEFAULT);ImageView.setImageBitmap(BitmapFactory.decodeByteArray(imageBytes 0 imageBytes.length)

#4


0  

i checked it. it's working fine.

我检查它。这是工作正常。

may be your passing wrong "encodedString".

可能是您传递错误的“encodedString”。

test the below code

测试以下代码

String strBase64= "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAFA3PEY8MlBGQUZaVVBfeMiCeG5uePWvuZHI////////////////////////////////////////////////////2wBDAVVaWnhpeOuCguv/////////////////////////////////////////////////////////////////////////wAARCABeAJoDASIAAhEBAxEB/8QA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qjyanoouBX8k0oAjVgTyanqGSMs/Hf3pMTEKhwuDnAo8oUsaFX5/nUuKECIdnvQFI71Nim4p3HYbg0Yp+KTFAhmKMU/FJigBlVqsuwUc1WoZLJYdufmp8yoeU49qgU4pSxPGaRFtRKnhnCrtYE1BUsRCg5HPrQVexI1yv8IP41JE+9MkjPeqbdaccKqsrHd3pD5i7SVXFw/dc09Zlbvg+9LUu6JGIUc0wOu4k8YFJNygP5VEIc9wD70C6kxdQT705SGGRVfyvQg/SpYxtTIPFAIkpKjaUKM5z+NM88/wB2mO6J6aWCjkiq6uzuAWIzTW+8cnPvTJ5h7TOTxwKjyT3pzMCAABxTaCb3DvzTadSUAApaQHFLuoAKATjFG6jd7UALg0nWjdRu9qBCjIowaTdRuoAdkjHtT9ynlsk1Fuo3Uhkm4AfLkUm44I9aZuo3UAKcmg5OKTdRupiAZFHJo3UbqBhiijdSZoAWm0uaKAP/2Q==";

字符串strBase64 =“/ 9 j / 4 aaqskzjrgabaqaaaqabaad / 2 wbdafa3pey8mlbgquzavvbfemiceg5uepwvuzhi / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / 2 wbdavvawnhpeoucguv / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / wAARCABeAJoDASIAAhEBAxEB / 8 qahwaaaqubaqebaqeaaaaaaaaaaaecawqfbgcicqol / 8 qatraaagedawieawufbaqaaaf9aqidaaqrbrihmuege1fhbyjxfdkbkaeii0kxwrvs0fakm2jyggkkfhcygroljicokso0nty3odk6q0rfrkdisuptvfvwv1hzwmnkzwznaglqc3r1dnd4exqdhiwgh4ijipktljwwl5izmqkjpkwmp6ipqrkztlw2t7i5usldxmxgx8jjytlt1nxw19jz2uh当+ Tl5ufo6erx8vP09fb3 + Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL / 8 qatreaagecbaqdbacfbaqaaqj3aaecaxeebsexbhjbuqdhcrmimoeifekrobhbcsmzuvavynlrchyknoel8rcygromjygpkju2nzg5okneruzhselku1rvvldywvpjzgvmz2hpann0dxz3ehl6gooehyahiimkkpoulzaxmjmaoqokpaanqkmqsro0tba3ulm6wspexcbhymnk0tpu1dbx2nna4upk5ebn6onq8vp09fb3 + Pn6/9oADAMBAAIRAxEAPwCsqgrnFLsHpQn3RTqoY3YPSjYPSnUUAN2L6UbF9KfRQAzYvpRsX0p9FADNi + lGxfSn0UAM2L6UbB6U / FJQIbsHpRsHpTqKAG7R6Um0elPpKYDdg9Kd5YpVqbhEU7QSfWiyIbK5THao6suBk4BA9DVWlJDi7kyfcFOpqH5BS7vagsWik3UbqAFpaTNLmgAopNwo3CgBaQ5A460m + lQlnAoewAM9 + tFDkhyOKbuPtQtgHUUmaOaYC0lJ83rRz60CHDinrIyjAIx7iosmjJoE1cezZySck1WqbJqGlIErD1 + 7 s0l90uufbsu6jfawahfgamumkasjrs4ooaspifv / AIUypYPvE + 1 j7anlhz1hus33h9kaka2abztlnlg9qagphyummyjqtaanppbyjcmny9axbs4ocw3fqvzxvakxei4x1pwxptmohtgspykvxpknlt6ubg9kesyec8gpqaertuxoqhgpajy3pu + KMUXAh8qjyanoouBX8k0oAjVgTyanqGSMs / Hf3pMTEKhwuDnAo8oUsaFX5 nUuKECIdnvQFI71Nim4p3HYbg0Yp + KTFAhmKMU / FJigBlVqsuwUc1WoZLJYdufmp8yoeU49qgU4pSxPGaRFtRKnhnCrtYE1BUsRCg5HPrQVexI1yv8IP41JE + 9 mkjpeqbdaccKqsrHd3pD5i7SVXFw / dc09Zlbvg + 9 luu6jgiuc0wou4k8yfjnygp5veic9wd70c6kxdqt705sggrvfyvqg / SpYxtTIPFAIkpKjaUKM5z + NM88 / wB2mO6J6aWCjkiq6uzuAWIzTW + 8 cnpvtj5h7totxwkjyt3pzmcaabxtacb3dvztadsuaapaqhfluoakatjfg6jd7ualg0nwjdru9qbcjiowatdruoadkjhtt9ynlsk1fuo3uhkm4aflkum44i9azuo3uakcmg5oktdrupiazfhjo3ubqbhiijdszoawm0uakap / 2 q = =”;

    Log.d("base64 String: ", strBase64);


    byte[] decodedString = Base64.decode(strBase64, Base64.DEFAULT);
    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
    test.setImageBitmap(decodedByte);