【发布时间】:2016-04-19 23:42:41
【问题描述】:
我尝试使用 Firebase 列表适配器在列表视图中检索图像。我正在使用 POJO 类从 firebase 保存和检索图像。
下面是我的 POJO 类:Data.java
public class Data {
String imag;
public Data(){
}
public Data(String imag){
this.imag = imag;
}
public String getImag() {
return imag;
}
public void setImag(String imag) {
this.imag = imag;
}
}
图片存储代码:
Firebase ref = new Firebase("https://imaglist.firebaseio.com");
Bitmap bm = BitmapFactory.decodeFile(imgDecodableString);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG,100,baos);
byte[] byteArray = baos.toByteArray();
encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
Data d = new Data();
d.setImag(encodedImage);
ref.child("Photo").push().setValue(d, new Firebase.CompletionListener() {
@Override
public void onComplete(FirebaseError firebaseError, Firebase firebase) {
if(firebaseError != null){
Toast.makeText(getApplicationContext(),firebaseError.getMessage(),Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(),"Image Saved",Toast.LENGTH_SHORT).show();
}
}
});
检索图像的代码:
list = (ListView)findViewById(R.id.list);
img = (ImageView)findViewById(R.id.img);
FirebaseListAdapter<Data> ad = new FirebaseListAdapter<Data>(MainActivity.this,Data.class,R.layout.row,ref) {
@Override
protected void populateView(View view, Data data, int i) {
Data d = new Data();
byte[] dec = Base64.decode(encodedImage,Base64.DEFAULT);
Bitmap decodeByte = BitmapFactory.decodeByteArray(dec, 0, dec.length);
img.setImageBitmap(decodeByte);
}
};
list.setAdapter(ad);
如何在 firebase 列表适配器中设置 getter 以获取图像。
【问题讨论】:
-
我推荐你使用图片作为String(url),然后使用Glide框架来展示它们;)
-
我如何在上面的代码中使用 getter 方法来做到这一点,因为我已经在 Base64 字符串中转换了图像。
-
您能否提供一个代码如何使用 glide 从 firebase 检索图像。
-
Firebase 刚刚发布了一项名为 Firebase Storage 的新功能。这允许您将图像和其他非 JSON 数据上传到专用存储服务。我们强烈建议您使用它来存储图像,而不是将它们作为 base64 编码数据存储在 JSON 数据库中。 Android 文档还包含如何使用 Glide 显示这些图像的示例。