【发布时间】:2018-08-31 13:24:59
【问题描述】:
伙计们,我连续 2 天都面临这个问题。我想从图库中挑选一张图片并将其转换为 Base64 方法。我试过毕加索,但我的图像视图很大。你能帮我么。我尝试了所有方法,但是在将其转换为位图然后再转换为 base64 时,内存不足对我来说太多了。
BitmapDrawable drawable = (BitmapDrawable) ProfilePic.getDrawable();
yourSelectedIBitmapDrawable drawable = (BitmapDrawable) ProfilePic.getDrawable();
yourSelectedImage = drawable.getBitmap();mage = drawable.getBitmap();
将此位图转换为 Base64 的代码。有没有可能跳过位图直接转成Base64
private String encodeToBase64(Bitmap image) {
Bitmap immagex = image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);
//Log.e("LOOK", imageEncoded);
return imageEncoded;
}
Profile Pic 是 Imageview 的名称。
编辑:因此,guyz 的一种方法是使用大堆,但 Google 表示,除非绝对必要,否则不应使用它。另一个对我有用的是公认的答案。现在我想出了自己的解决方案。理论上,我正在使用 Picasso 将图像加载到图像视图中。那么如果我可以从 ImageView 中提取图像呢?不是来自 URI 或路径,而是来自 Imageview。这样你就有了一幅已经被毕加索缩小的图像。 Picasso 为成功或失败提供 回调。所以在 Success 时,你可以访问 ImageView 的缓存并从中提取 Bit map。
我会尽快发布代码作为答案。我试过了,即使是原来的35Mb的图像也可以转换为位图而不会出现内存不足的异常。
【问题讨论】:
-
你使用了大堆吗?
-
一种方法是使用它。和其他被接受的一个。但最好的是使用毕加索。等等,我会发布我的代码。
标签: android bitmap base64 out-of-memory