如何将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
最满意答案
默认情况下,使用
#[]
将检索哈希值(如果存在),如果不存在则返回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 aKeyError
if it doesn'tfetch(key_name, default_value)
: get the value if the key exists, returndefault_value
otherwisefetch(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 tonil
. 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 ...
-
等于(=)vs. LIKE(Equals(=) vs. LIKE)[2022-05-19]
不同的运营商 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 ...
-
Ruby Hashes vs. Arrays:查找项目的最快方法?(Ruby Hashes vs. Arrays: quickest way to find an item?)[2022-05-30]
是的,你可以使用一个数组,最好是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); ...
-
Perl DBM vs. Storable(Perl DBM vs. Storable)[2023-12-30]
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 ...
-
JOIN vs. IN vs. EXISTS(JOIN vs. IN vs. EXISTS)[2021-12-26]
正如我所看到的,在很多情况下你可以将它们用作同样的东西,但你不能忘记它们背后的细节。 也许你可以同时应用NOT IN和NOT EXISTS得到相同的结果,但你可以看到查询中涉及NULL值的差异。 因为NOT EXISTS是获取具有NULL值的那些行的唯一方法。 在这个例子中你可以更好地看到它: update cars set c_owner = NULL where c_id = BMW03444 嗯......我们试着看看我们是否有任何尚未售出的车库存。 select count(*) from car ...