我尝试使用Firebase列表适配器在listview中检索图像。我正在使用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以获取图像。
答案 0 :(得分:3)
你可以使用存储在Firebase上的JSON,保存url-images(例如位于Imgur上),然后使用Glide作为图像检索,没有任何复杂情况,futurestud.io网站,例如:
ImageView targetImageView = (ImageView) findViewById(R.id.imageView);
String internetUrl = "http://i.imgur.com/DvpvklR.png";
Glide
.with(context)
.load(internetUrl)
.into(targetImageView);
相信我,使用Firebase非常简单