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;
}
}
这总是返回null,即使我给它编码64(utf-8)字符串---> aGVsbG8 =
为什么这种情况发生在任何人都有想法.. ??我做错了可以任何人建议我......
答案 0 :(得分:1)
我认为问题在于您尝试将base64字符串解码为Bitmap,但实际上您只是想将其解码为字符串。这是执行此操作的代码:
String decodeBase64String(String encodedString)
{
byte[] data = Base64.decode(encodedString, Base64.DEFAULT);
return new String(data, "UTF-8");
}
(假设采用UTF-8编码)
如果你用这样的测试字符串调用这个函数:
String result = decodeBase64String("aGVsbG8=");
然后结果将是“你好”。
以下是将文本转换为位图的方法:
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;
}
因此,您可以将base64编码的文本转换为如下位图:
String result = decodeBase64String("aGVsbG8=");
Bitmap bitmap = textToBitmap(result);
或者你可以这样做:
Bitmap bitmap = textToBitmap("hello");
答案 1 :(得分:1)
您可以使用其他一些内置方法恢复您的代码。
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)
答案 2 :(得分:0)
试试这个,可能对你有帮助..
String base =" ****你的Base64字符串******“;
byte [] imageBytes = Base64.decode(base.getBytes(),Base64.DEFAULT); ImageView.setImageBitmap(BitmapFactory.decodeByteArray(imageBytes,0,imageBytes.length)
答案 3 :(得分:0)
可能是你传球错误" encodedString"。
测试以下代码
String strBase64 =" / 9j / 4AAQSkZJRgABAQAAAQABAAD / 2wBDAFA3PEY8MlBGQUZaVVBfeMiCeG5uePWvuZHI ////////////////////////////////// ////////////////// 2wBDAVVaWnhpeOuCguv /////////////////////////////// ////////////////////////////////////////// wAARCABeAJoDASIAAhEBAxEB / 8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL / 8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4 + Tl5ufo6erx8vP09fb3 + PN6 / 8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL / 8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3 + PN6 / 9oADAMBAAIRAxEAPwCsqgrnFLsHpQn3RTqoY3YPSjYPSnUUAN2L6UbF9KfRQAzYvpRsX0p9FADNi + lGxfSn0UAM2L6UbB6U / FJQIbsHpRsHpTqKAG7R6Um0elPpKYDdg9Kd5YpVqbhEU7QSfWiyIbK5THao6suBk4 BA9DVWlJDi7kyfcFOpqH5BS7vagsWik3UbqAFpaTNLmgAopNwo3CgBaQ5A460m + lQlnAoewAM9 + tFDkhyOKbuPtQtgHUUmaOaYC0lJ83rRz60CHDinrIyjAIx7iosmjJoE1cezZySck1WqbJqGlIErD1 + 7S0L90UUFBSU6jFAWAHFGaMUmKAsJRS4ooASpIfv / AIUypYPvE + 1J7ANlHz1HUs33h9KaKa2AbzTlNLg9qAGphYUmmYJqTaaNppBYjCmnY9aXbS4oCw3FQVZxVakxEi4x1pwXPTmohTgSpyKVxpknlt6UbG9KesyEc8GpQAeRTuXoQhGPajY3pU + KMUXAh8qjyanoouBX8k0oAjVgTyanqGSMs / Hf3pMTEKhwuDnAo8oUsaFX5 / nUuKECIdnvQFI71Nim4p3HYbg0Yp + KTFAhmKMU / FJigBlVqsuwUc1WoZLJYdufmp8yoeU49qgU4pSxPGaRFtRKnhnCrtYE1BUsRCg5HPrQVexI1yv8IP41JE + 9MkjPeqbdaccKqsrHd3pD5i7SVXFw / dc09Zlbvg + 9LUu6JGIUc0wOu4k8YFJNygP5VEIc9wD70C6kxdQT705SGGRVfyvQg / SpYxtTIPFAIkpKjaUKM5z + NM88 / wB2mO6J6aWCjkiq6uzuAWIzTW + 8cnPvTJ5h7TOTxwKjyT3pzMCAABxTaCb3DvzTadSUAApaQHFLuoAKATjFG6jd7UALg0nWjdRu9qBCjIowaTdRuoAdkjHtT9ynlsk1Fuo3Uhkm4AfLkUm44I9aZuo3UAKcmg5OKTdRupiAZFHJo3UbqBhiijdSZoAWm0uaKAP / 2Q ==&#34 ;;
Log.d("base64 String: ", strBase64);
byte[] decodedString = Base64.decode(strBase64, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
test.setImageBitmap(decodedByte);