【问题标题】:Can I store image files in firebase using Java API我可以使用 Java API 将图像文件存储在 Firebase 中吗
【发布时间】:2014-12-05 05:49:10
【问题描述】:

有没有办法使用 Java api 将图像文件存储在 firebase 中?

【问题讨论】:

  • 如何转换回位图?
  • @Binghammer 这很简单。 byte[] imageAsBytes = Base64.decode(imageFile, Base64.DEFAULT);位图 bmp = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
  • Firebase 刚刚发布了一项名为 Firebase Storage 的新功能。这允许您将图像和其他非 JSON 数据上传到专用存储服务。我们强烈建议您使用它来存储图像,而不是将它们作为 base64 编码数据存储在 JSON 数据库中。

标签: java android firebase


【解决方案1】:

尝试使用这个 sn-p:

Bitmap bmp =  BitmapFactory.decodeResource(getResources(), R.drawable.chicken);//your image
ByteArrayOutputStream bYtE = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, bYtE);
bmp.recycle();
byte[] byteArray = bYtE.toByteArray();
String imageFile = Base64.encodeToString(byteArray, Base64.DEFAULT); 

用于检索

public Bitmap stringToBitMap(String encodedString){
   try {
      byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
      Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
      return bitmap;
   } catch(Exception e) {
      e.getMessage();
      return null;
   }
}

【讨论】:

  • 如何从 imageFile 字符串显示图像?
  • 以下内容可以帮助您将编码字符串显示为位图。 byte[] decodeString = Base64.decode(imageFileString, Base64.DEFAULT);位图位图 = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); imageView.setImageBitmap(位图);
猜你喜欢
  • 2014-12-19
  • 2015-07-15
  • 1970-01-01
  • 1970-01-01
  • 2021-04-22
  • 1970-01-01
  • 2020-03-17
  • 2016-12-31
  • 2021-08-19
相关资源
最近更新 更多