且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 Firebase 检索图像以显示在我的图像视图中

更新时间:2023-11-29 15:48:04

试试这个:

public class ShowImagesActivity extends AppCompatActivity {
//recyclerview object
private RecyclerView recyclerView;

//adapter object
private RecyclerView.Adapter adapter;

//database reference
private DatabaseReference mDatabase;

//progress dialog
private ProgressDialog progressDialog;

//list to hold all the uploaded images
private List<Upload> uploads;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_images);


    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));


    progressDialog = new ProgressDialog(this);

    uploads = new ArrayList<>();

    //displaying progress dialog while fetching images
    progressDialog.setMessage("Please wait...");
    progressDialog.show();
    mDatabase = FirebaseDatabase.getInstance().getReference(Constants.DATABASE_PATH_UPLOADS);

    //adding an event listener to fetch values
    mDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            //dismissing the progress dialog 
            progressDialog.dismiss();

            //iterating through all the values in database
            for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                Upload upload = postSnapshot.getValue(Upload.class);
                uploads.add(upload);
            }
            //creating adapter
            adapter = new MyAdapter(getApplicationContext(), uploads);

            //adding adapter to recyclerview
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            progressDialog.dismiss();
        }
    });

}
}

请打开此链接,它对您非常有用:

Please open this link it is very useful to you:

https://www.simplifiedcoding.net/firebase-storage-example/一个>