我有一个代表位图图像的Base64字符串.

我需要再次将该字符串转换为位图图像,以便在Android应用程序的ImageView上使用它

怎么做?

这是我用来将图像转换为base64字符串的代码:

//proceso de transformar la imagen BitMap en un String:
//android:src="c:\logo.png"
Resources r = this.getResources();
Bitmap bm = BitmapFactory.decodeResource(r, R.drawable.logo);
ByteArrayOutputStream baos = new ByteArrayOutputStream();  
bm.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object   
byte[] b = baos.toByteArray();
//String encodedImage = Base64.encode(b, Base64.DEFAULT);
encodedImage = Base64.encodeBytes(b);

推荐答案

基本上可以使用其他一些内置方法还原代码.

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

Android相关问答推荐

房间数据库迁移未正确处理:原因:java.lang. IllegalState异常:

将Android Studio插件复制到离线网络

推断的类型是片段,但应为上下文

在模块中找到重复的类com.google.Firebase.auth.ktx.AuthKt||Android Studio

DocumentFile.canWrite()、DocumentFile.Existes()-使用本地内置手机存储(而不是云)时性能较差(占用太多CPU时间)

Android Kotlin ImageView内置于Kotlin ImageView中.适配器未按预期更新

AndroidX Media3 迁移指南

Android Compose - 为什么 Canvas 中的drawText在底部被切断而不是在顶部?

如何在同一行中滚动自定义布局和惰性列,就好像它们是一个组件一样

如何禁用自动登录 google play games services android unity?

Android Jetpack Compose全宽度抽屉优化

将 React Native 应用程序背景带到前台

Android Compose 为什么 Checkbox 在父对象可点击时需要 onCheckChanged?

在 AGP 8.0 中构建失败,无法应用插件realm-android. APIandroid.registerTransform已删除

如何在 React Native 中调试网络响应

react 从输入中找到路径'lib/arm64-v8a/libfbjni.so'的本机2个文件

如何在行/列/卡片 compose 中添加左边框

android 13 版本是否会影响 android 12 目标应用程序

单击登录按钮后从应用程序中退出

未使用的内容填充参数