【问题标题】:Converting bitmap into Base64 not encoding correctly in android.. I not getting image back when decoding在android中将位图转换为Base64未正确编码..解码时我没有取回图像
【发布时间】:2017-07-26 23:51:41
【问题描述】:

这是我的代码,我已经尝试了很多代码种类...

public String getStringImage(Bitmap bmp){ 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
    byte[] imageBytes = baos.toByteArray(); 
    String encodedImage = Base64.encodeToString(imageBytes,Base64.DEFAULT); 
    return encodedImage; 
}

【问题讨论】:

  • 任何人都得到了答案......??
  • 请参考此链接enter link description here
  • 是的,先生,我也试过了......但结果是一样的......正在转换,但我认为它的格式不正确......
  • 您需要Base64.encodeToString 做什么?为什么不能使用原始字节?
  • 我需要把这个文件上传到mysql..

标签: android bitmap base64


【解决方案1】:

您可以尝试以下代码对图像进行编码和解码:

//Compress img and save
private String encodeBitmapAndSave(Bitmap bitmap1) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap1.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
}

//Decompress img 
private Bitmap decodeFromFirebaseBase64(String image) throws IOException {
    byte[] decodedByteArray = Base64.decode(image, Base64.DEFAULT);
    return BitmapFactory.decodeByteArray(decodedByteArray, 0, decodedByteArray.length);
}

【讨论】:

  • 是的!但是您没有发布如何解码它。并确保您的位图不为空。
  • 我检查了位图是否正常显示。但编码的字符串格式不正确
  • 你好....编码是正确的,我可以从同一活动中解码该图像并获取图像...但是我需要将此数据字符串上传到我的sql中...但是上传时如果原始图像大小小于 100 kb,但在 mysql 中显示为 1mb 左右.. 请帮忙。
  • @Sayandh 您在将 Base64 保存到 mysql 时一定做错了什么。您应该使用blobe 类型,如图所示here
  • 数据库上的数据类型是 currect 是中等 blob... 我正在使用 nameValuepair 将数据作为字符串发送到 php 页面...
猜你喜欢
  • 1970-01-01
  • 2011-04-27
  • 2013-03-18
  • 1970-01-01
  • 1970-01-01
  • 2015-07-12
  • 1970-01-01
  • 2016-08-26
  • 2018-11-22
相关资源
最近更新 更多