【发布时间】:2021-02-01 09:28:28
【问题描述】:
我想显示来自 Firebase 实时数据库的图像。但我认为它不能在 MainUpload.java 中运行 loadPhoto()
Oncreate(){
mRecyclerView = findViewById(R.id.recyclerview_main_list);
int numberOfColumns = 3;
GridLayoutManager mGridLayoutManager = new GridLayoutManager(getApplication(),numberOfColumns);
mRecyclerView.setLayoutManager(mGridLayoutManager);
mRecyclerView.setHasFixedSize(true);
mItem = new ArrayList<>();
myAdapter = new MyAdapter(mItem);
mRecyclerView.setAdapter(myAdapter);
//clearAll();
loadPhoto();
DividerItemDecoration dividerItemDecoration=new DividerItemDecoration(mRecyclerView.getContext(),mGridLayoutManager.getOrientation());
mRecyclerView.addItemDecoration(dividerItemDecoration);
mRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), mRecyclerView, new ClickListener() {
@Override
public void onClick(View view, int position) {
ItemObject itemObject = mItem.get(position);
Intent intent = new Intent(getApplicationContext(), DetailActivity.class);
intent.putExtra("title",itemObject.getTitle());
intent.putExtra("photo",itemObject.getPhoto());
startActivity(intent);
}
}
loadPhoto() 函数
private void loadPhoto() {
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
String key = FirebaseDatabase.getInstance().getInstance().getReference("users")
.child(currentUser.getUid()).child("Object").getKey();
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("users")
.child(currentUser.getUid()).child("Object").child(key);
if(databaseReference !=null) {
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
ItemObject itemObject = new ItemObject();
//여기서 에러
AESCoderAndriod aesCoderAndriod = new AESCoderAndriod();
try {
String En1 = snapshot.child("photo").getValue().toString();
byte[] En2 = En1.getBytes();
byte[] Dn1 = aesCoderAndriod.decrypt(seed, En2);
Bitmap Dn2 = byteArrayToBitmap(Dn1);
Uri Dn3 = getImageUri(getApplication(), Dn2);
itemObject.setPhoto(Dn3.toString());
itemObject.setTitle(snapshot.child("title").getValue().toString());
mItem.add(itemObject);
Log.e("test", Dn3.toString());
} catch (Exception e) {
e.printStackTrace();
}
// itemObject.setPhoto(snapshot.child("Photo").getValue().toString());
}
//myAdapter= new MyAdapter(getApplicationContext(),mItem);
// mRecyclerView.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
ItemObject.java
public class ItemObject {
private String title;
private String Photo;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPhoto() {
return Photo;
}
public void setPhoto(String photo){
this.Photo=photo;
}
public ItemObject(String title, String photo) {
this.title = title;
this.Photo=photo;
}
public ItemObject() {}
}
【问题讨论】:
-
首先,停止忽略错误。使用
Log.d(TAG, databaseError.getMessage());。您是否在 logcat 中打印了一些内容? -
"我认为它不能在 MainUpload.java 中运行 loadPhoto()" 为什么不呢?当您在调试器中运行此代码并逐行执行时,哪一行没有按照您的预期执行?
标签: android firebase-realtime-database android-recyclerview