2014-10-10 66 views
8

有没有什么方法可以使用Java api将图像文件存储在Firebase中?我可以使用Java API将图像文件存储在firebase中

+0

如何在转换回位图? – 2014-10-30 23:16:29

+2

@Binghammer很简单。 byte [] imageAsBytes = Base64.decode(imageFile,Base64.DEFAULT); Bitmap bmp = BitmapFactory.decodeByteArray(imageAsBytes,0,imageAsBytes.length); – skabir 2014-11-03 02:39:16

+2

* Firebase刚发布了一项名为[Firebase Storage]的新功能(https://firebase.google.com/docs/storage/)。这允许您将图像和其他非JSON数据上传到专用存储服务。我们强烈建议您使用它来存储图像,而不是将它们作为base64编码数据存储在JSON数据库中。* – 2016-05-20 04:27:16

回答

11

尝试使用这段代码:

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; 
    } 
} 
+3

如何从imageFile字符串显示图像? – 2014-10-30 23:16:01

+3

以下内容可帮助您将编码的字符串显示为位图。 byte [] decodedString = Base64.decode(imageFileString,Base64.DEFAULT); 位图位图= BitmapFactory.decodeByteArray(decodedString,0,decodedString.length); imageView.setImageBitmap(bitmap); – ernestkamara 2016-06-12 11:57:06

相关问题