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bAAaNcFREACIg00TANiDDsiESgSP4wcx/PfLFuKpK2UqgFQb7z6Gi4cexo7n3wK2LZSgwPQ4RbSspTq7KTdcwhtI8/IW+fO6guXLgL7DdNmLy8v73hPp1sHB6CODii6BixXGP3og2Jw6NijAGpePQDgQOTrr3Zx6aINAIuLi9v2ysqK/0IIWpaLYpEsF+4Spiy7+eMA/HYnAA2gHrQsF4ANAKurq46dyWSsRCJhcrmcs3PnL4V3TveGbavIctl2jh5xvcPKAykAzOfzNgAmk0lmMhkBgG1jTDwajdbj8bghGSVpk9QklSeQtDzZyWSyFg6HqyQfBlBBNptVJO2ZmZk/AJhCoRDwnHUTQHuyS6WSA8BMT09fIxnLZrM2vP7XJE/Ek6lGIBSVplv9SLQPDkU6qh1dPTWSh0QkRtLy/hM6JCu8c84MdkGANrP246/hpgisKz9dHQfay0ficHn7/TLJ70TYIyL2/qeiVKCaOVm07122nHhIZ68LznxZ5W83HQY7H0c1v6GGu12enghy8ElNc69i3IdeuB588fI4yJ1/AG/UgW4Mx7v6AAAAAElFTkSuQmCC"; //将Base64编码字符串解码成Bitmap String outputStr; outputStr = ""; if (!base64String.isEmpty()) { //移除所有换行和空格 outputStr = base64String.replace("data:image/png;base64,",""); //如果有data:image/png;base64字段则去除 byte[] decodedString = new byte[14096]; decodedString = Base64.decode(outputStr, Base64.DEFAULT); int zfgs= outputStr.length(); String s = String.valueOf(zfgs); textdx1.setText("字符个数为:" + s + ",已经移除换行符: "+base64String); //return; } byte[] tpxssj = new byte[14096]; tpxssj =outputStr.getBytes(); decodedByte = BitmapFactory.decodeByteArray(tpxssj, 0, tpxssj.length); //显示ImageView图片 tpczrq.setImageBitmap(decodedByte); int aa =1; if(aa==1) return;
时间: 2024-02-14 19:14:10 浏览: 133
base64字符转换和图片的转换
5星 · 资源好评率100%
根据你提供的代码,似乎你想使用 Base64 编码的图片数据来显示一个 ImageView。但是,我发现你在解码 Base64 字符串时移除了 "data:image/png;base64," 这个前缀,这可能是导致显示图片为空的原因之一。
另外,你获取的字节数组长度为 14096,但这个长度可能会因为编码方式而有所不同,所以建议使用 decodedString.length 来确定字节数组的长度。
下面是一些可能有用的修改建议:
```java
String base64String = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABEAAAAUCAYAAABroNZJAAAA4WlDQ1BzUkdCAAB4nGNgYDzNAARMDgwMuXklRUHuTgoRkVEKDEggMbm4gAE3YGRg+HYNRDIwXNYNLGHlx6MWG+AsAloIpD8AsUg6mM3IAmInQdgSIHZ5SUEJkK0DYicXFIHYQBcz8BSFBDkD2T5AtkI6EjsJiZ2SWpwMZOcA2fEIv+XPZ2Cw+MLAwDwRIZY0jYFhezsDg8QdhJjKQgYG/lYGhm2XEWKf/cH+ZRQ7VJJaUQIS8dN3ZChILEoESzODAjQtjYHh03IGBt5IBgbhCwwMXNEQd4ABazEwoEkMJ0IAAHLYNoQhqzLMAAAACXBIWXMAAA7EAAAOxAGVKw4bAAADlklEQVQ4jW2UXWhcRRTH/zP33u1udpPNJm6zq5g2WYyxaWowUpBGUbQPfkDSQEAUH3zLw0JepT4JtRUkGKT4IBGC1HwYExHxoZYuoSoiVpCWNYQEW9paNjTZbHdxv+7s+fuQe2UtDvxnDsOZ35xz5jAQETSLZGR9ff2RsbGxeiwWqwGot7e3u6Ojo/lsNvsYyaCIKE8QETwICE5NTQmABgB/bZak02khGf9fCEk9Nzd3HwAByOTkpNvf3+8CML29vfV0Ou16YJmdnd0jafkgHwCSYQ/A7u5uIelmMhkXQL1YLNZJuqlUSnwfkgGSSkSgsT+spaWlu56NaDSKUqnEhYUFAMDGxgZ3d3eltbWVvs/8/Pxnvv1vKhMTE8a/BYB8+PGcefnU2waAefbkRGP6/Od+nQiA4+Pj4keyPyll9fX1uZubmyraonH2TQeHYzUcTgDxWAg7exXcyAE3Cwfw7hcGhb8bSKVS3NraskjS9gLSt/f2FADcrxBGNF45EQY6BHCBeCqEJwoa579toFAWAMCtfB4PpmOd6UjwKe2wKSVCg20HQVj4z/6Qdvhe9CBJBkQEiqQyv197yR1+7vtQMgEohSoFfxoX265BWYAWDXQ5NnotB0GtARKVXI7OL2tle3gookiqtVOvV0au/ByAUmrPuNAtLbAAaNcFREACIg00TANiDDsiESgSP4wcx/PfLFuKpK2UqgFQb7z6Gi4cexo7n3wK2LZSgwPQ4RbSspTq7KTdcwhtI8/IW+fO6guXLgL7DdNmLy8v73hPp1sHB6CODii6BixXGP3og2Jw6NijAGpePQDgQOTrr3Zx6aINAIuLi9v2ysqK/0IIWpaLYpEsF+4Spiy7+eMA/HYnAA2gHrQsF4ANAKurq46dyWSsRCJhcrmcs3PnL4V3TveGbavIctl2jh5xvcPKAykAzOfzNgAmk0lmMhkBgG1jTDwajdbj8bghGSVpk9QklSeQtDzZyWSyFg6HqyQfBlBBNptVJO2ZmZk/AJhCoRDwnHUTQHuyS6WSA8BMT09fIxnLZrM2vP7XJE/Ek6lGIBSVplv9SLQPDkU6qh1dPTWSh0QkRtLy/hM6JCu8c84MdkGANrP246/hpgisKz9dHQfay0ficHn7/TLJ70TYIyL2/qeiVKCaOVm07122nHhIZ68LznxZ5W83HQY7H0c1v6GGu12enghy8ElNc69i3IdeuB588fI4yJ1/AG/UgW4Mx7v6AAAAAElFTkSuQmCC";
//将Base64编码字符串解码成Bitmap
String outputStr = base64String;
if (!base64String.isEmpty() && base64String.contains("data:image/png;base64,")) {
//移除前缀
outputStr = base64String.replace("data:image/png;base64,", "");
}
byte[] decodedString = Base64.decode(outputStr, Base64.DEFAULT);
//显示字符个数
textdx1.setText("字符个数为:" + outputStr.length() + ",已经移除前缀: "+ base64String);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
//显示ImageView图片
tpczrq.setImageBitmap(decodedByte);
```
希望这些修改能够帮助你解决问题。
阅读全文