从Firebase数据库检索图像并将其显示在我的应用程序的屏幕上

时间:2019-03-23 15:49:13

标签: java firebase-realtime-database

我正在尝试从Firebase数据库中获取图像以在Activity中显示在我的应用中。图像成功上传,但是当我尝试查看它们时,我看不到它们。这是我从firebase中检索它们的代码。 android studio。

public class ActionBar22 extends AppCompatActivity {

    private RecyclerView mrecyclerview;
    private ImageAdapter madapter;

    private ProgressDialog mprogresscircle;

    private DatabaseReference mdatabaseref;
    private List<UploadedPosts> muploads;

    List<UploadedPosts> list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action_bar22);


       //pull items from firebase database
       mrecyclerview = findViewById(R.id.recycler_view);
        mrecyclerview.setHasFixedSize(true);
       mrecyclerview.setLayoutManager(new LinearLayoutManager(this));

        mprogresscircle = findViewById(R.id.progress_circle);
        mprogresscircle = new ProgressDialog(ActionBar22.this);
        mprogresscircle.setMessage("Loading...");
        mprogresscircle.show();

        muploads = new ArrayList<>();

      final FirebaseDatabase database = FirebaseDatabase.getInstance();
       DatabaseReference ref  = database.getReference("Uploads");

                for(DataSnapshot postsnapshot : dataSnapshot.getChildren()){
            UploadedPosts upload=postsnapshot.getValue(UploadedPosts.class);
                         muploads.add(upload);
                     }

                     madapter = new ImageAdapter(ActionBar22.this,muploads);

                     mrecyclerview.setAdapter(madapter);
                     mprogresscircle.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Toast.makeText(ActionBar22.this, databaseError.getMessage(),Toast.LENGTH_LONG).show();
                mprogresscircle.dismiss();
                mprogresscircle.setVisibility(View.INVISIBLE);
            }
        });     
    }
}

我希望它在屏幕的宽度上显示一张照片,它们以一列显示。这些照片不显示。仅空白屏幕。enter image description here

1 个答案:

答案 0 :(得分:0)

使用Glide或Picasso在ImageViews中显示图片,这是如何实现的示例:

成绩:

dependencies {
    compile 'com.github.bumptech.glide:glide:3.5.2'
    compile 'com.android.support:support-v4:22.0.0'
}

代码:

   Glide
                .with(context)
                .load(yourImageURLHere)
                .apply(RequestOptions.signatureOf(new CenterCrop()))
                .into(ImageView);

,在这里您可以找到其他示例:https://github.com/bumptech/glide,向下滚动页面并进行检查。