首页 \ 问答 \ 如何将Base64字符串转换为BitMap图像以在ImageView中显示?(How to convert a Base64 string into a BitMap image to show it in a ImageView?)

如何将Base64字符串转换为BitMap图像以在ImageView中显示?(How to convert a Base64 string into a BitMap image to show it in a ImageView?)

我有一个Base64字符串表示一个BitMap图像。

我需要再次将该String转换为BitMap图像,以便在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);

I have a Base64 String that represents a BitMap image.

I need to transform that String into a BitMap image again to use it on a ImageView in my Android app

How to do it?

This is the code that I use to transform the image into the base64 String:

//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);

原文:https://stackoverflow.com/questions/4837110
更新时间:2022-10-15 13:10

最满意答案

默认情况下,使用#[]将检索哈希值(如果存在),如果不存在则返回nil。

使用#fetch提供几个选项(请参阅#fetch上的文档):

  • fetch(key_name) :如果该键存在,则获取该值,如果不存在则引发一个KeyError
  • fetch(key_name, default_value) :如果键存在,则获取该值,否则返回default_value
  • fetch(key_name) { |key| "default" } fetch(key_name) { |key| "default" } :如果键存在,获取值,否则运行提供的块并返回值。

应该根据情况需要使用每个,但#fetch功能非常丰富,可以根据使用情况处理许多情况。 因此,我倾向于使用#[]访问密钥。

*正如Marc-AndréLafortune所说,访问#[]的密钥将会调用#default_proc如果存在),否则返回#default ,默认为nil 。 有关更多信息,请参阅::new的文档条目 。


By default, using #[] will retrieve the hash value if it exists, and return nil if it doesn't exist *.

Using #fetch gives you a few options (see the docs on #fetch):

  • fetch(key_name): get the value if the key exists, raise a KeyError if it doesn't
  • fetch(key_name, default_value): get the value if the key exists, return default_value otherwise
  • fetch(key_name) { |key| "default" }: get the value if the key exists, otherwise run the supplied block and return the value.

Each one should be used as the situation requires, but #fetch is very feature-rich and can handle many cases depending on how it's used. For that reason I tend to prefer it over accessing keys with #[].

* As Marc-André Lafortune said, accessing a key with #[] will call #default_proc if it exists, or else return #default, which defaults to nil. See the doc entry for ::new for more information.

相关问答

更多
  • 默认情况下,使用#[]将检索哈希值(如果存在),如果不存在则返回nil。 使用#fetch提供几个选项(请参阅#fetch上的文档): fetch(key_name) :如果该键存在,则获取该值,如果不存在则引发一个KeyError fetch(key_name, default_value) :如果键存在,则获取该值,否则返回default_value fetch(key_name) { |key| "default" } fetch(key_name) { |key| "default" } :如果键存 ...
  • 从手册: :[论据]无影响; 该命令不会扩展参数并执行任何指定的重定向。 返回零退出代码。 因为这返回始终为零,因此类似于被用作true 看看这个答案: 什么是`:'(冒号)的目的GNU Bash内置? from manual: : [arguments] No effect; the command does nothing beyond expanding arguments and performing any specified redirections. A zero exit code is r ...
  • 不同的运营商 LIKE和=是不同的运算符。 这里的大多数答案都集中在通配符的支持上,这不是这些操作符之间的唯一区别! =是对数字和字符串进行操作的比较运算符。 比较字符串时,比较运算符比较整个字符串 。 LIKE是一个字符串运算符, 逐个字符比较。 使事务复杂化,两个运营商都使用对比较结果有重要影响的整理 。 激励例子 首先,我们首先确定这些运算符产生明显不同结果的示例。 让我引用MySQL手册: 根据SQL标准,LIKE在每个字符的基础上执行匹配,因此它可以产生与=比较运算符不同的结果: mysql> S ...
  • mipmap文件夹仅用于放置您的应用程序/启动器图标(显示在主屏幕上)。 您使用的任何其他可绘制资产应与以前一样放置在相关的可绘制文件夹中。 根据这个Google博客 : 最好的做法是将应用程序图标放在mipmap文件夹(而不是可绘制文件夹)中,因为它们的分辨率与设备的当前密度不同。 引用mipmap文件夹时,确保您使用以下参考: android:icon="@mipmap/ic_launcher" 他们使用不同密度的原因是,一些发射器实际上显示的图标大于它们的意图。 因此,他们使用下一个大小。 The ...
  • 你是对的。 $stmt->execute()仅在无法执行查询时返回false 。 使用空结果集时,它不会返回false 。 所以你需要使用fetch()来检查结果, fetch()为空结果集返回false 。 对于异常,只有在为方法execute()等设置异常模式时,PDO才会抛出异常。但无论是否设置了异常模式, new PDO(...)都将抛出异常。 Yes, you are right. $stmt->execute() return false only when it failed to execu ...
  • 是的,你可以使用一个数组,最好是O(logN) ,但使用一个集合更快更好。 require 'set' hashes = Set.new hashes << 'foo' hashes << 'bar' hashes.include?('bar') # => true 在ruby集中使用哈希表实现,因此查找是O(1) 。 Yes, you could use an array, which would be O(logN) at best, but it is both faster and semant ...
  • 以防它可以帮助某人。 我所做的是解决这个问题: 使用警报管理器设置重复警报,该警报将以特定间隔调用服务以获取数据。 类似下面的代码在应用程序第一次运行时调用。 Intent i = new Intent(WordListActivity.this, NewData.class); PendingIntent pint = PendingIntent.getService( WordListActivity.this, 0, i, 0); ...
  • SQLite正在迅速成为简单的磁盘数据库的标准。 在Perl中你可以使用DBD :: SQLite ,你很高兴。 SQLite is fast becoming the standard for simple on-disk databases. And in Perl you can just use DBD::SQLite and you're good to go.
  • $q = mysql_fetch_assoc(mysql_query("SELECT * FROM articles WHERE article_id = $id")); while($row = $q){ $data[] = $row; } 这个循环没有结束,因为你之前曾为$ q赋值,它永远不会改变。 因此,同一行会一遍又一遍地附加到$ data,最后您的脚本内存不足。 您必须分别为每个记录调用fetch函数(如第一个代码中所示)。 $q = mysql_fet ...
  • 正如我所看到的,在很多情况下你可以将它们用作同样的东西,但你不能忘记它们背后的细节。 也许你可以同时应用NOT IN和NOT EXISTS得到相同的结果,但你可以看到查询中涉及NULL值的差异。 因为NOT EXISTS是获取具有NULL值的那些行的唯一方法。 在这个例子中你可以更好地看到它: update cars set c_owner = NULL where c_id = BMW03444 嗯......我们试着看看我们是否有任何尚未售出的车库存。 select count(*) from car ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。