【问题标题】:Converting imageview to base64 and storing it into mysql and how to decode it in Android将imageview转换为base64并存入mysql以及如何在Android中解码
【发布时间】:2018-11-16 21:10:26
【问题描述】:

我已经使用 base64 对 imageview 进行了编码,并将该值传递给 textview,因为我已成功插入到 db 中。我要编码的代码:

public void convertImg(){
    imageView.buildDrawingCache();
    Bitmap bm = imageView.getDrawingCache();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
    byte[] b = baos.toByteArray();
    String encodedImage = Base64.encodeToString(b , Base64.DEFAULT);
    base64.setText(encodedImage);
}

这就是我在我的 mysql 数据库中得到的。这是正确的吗? 我的问题是如何解码它以及在哪里查看/查看它?

【问题讨论】:

标签: android mysql decode encode


【解决方案1】:

您的图像编码看起来正确。要将编码字符串解码回图像,您需要实现BitmapFactory

首先从编码字符串中获取字节。然后使用 BitmapFactory 对字节数组进行解码。 BitmapFactory.decodeByteArray 返回一个位图,然后您可以在 imageView 中使用。

byte[] b = Base64.decode(encodedString,Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(b,0,b.length);
imageView.setImageBitmap(bitmap);

【讨论】:

  • 我可以编写 php 代码来解码并在我的 localhost 网络浏览器中查看吗?
  • 我个人不知道,快速查看this thread,您可能会发现它有帮助。
【解决方案2】:

你必须实现 BitmapFactory.decodeByteArray() 来解码你的图片 base64 字符串看看这个。

//decode base64 string to image
imageBytes = Base64.decode(encodedImage , Base64.DEFAULT);
Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
mage.setImageBitmap(decodedImage);

希望对你有帮助!!

【讨论】:

  • 在图像中通过编码得到什么,对吗?
  • 你会看到图像!
  • 这可能对codesfor.in/…有帮助
【解决方案3】:

由于这被标记为 Android,我假设您指的是 sqlite?

不是您问题的直接答案,但在 sqlite 中,您可以存储二进制 blob 而无需先转换为 base64。在您的情况下,它可能更有效,因为转换为 base64 是有成本的。

https://www.sqlite.org/datatype3.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    • 2020-10-09
    • 2021-05-03
    • 1970-01-01
    相关资源
    最近更新 更多