将Firebase存储中的图像加载到图像视图中

时间:2019-05-04 15:51:33

标签: java android firebase firebase-realtime-database firebase-storage

在我的应用程序中,用户可以将文本和图像发送给其他用户,现在我还希望当用户单击已发送或接收的图像时,他们将被带到另一个活动中,即图像将被加载到图像视图中用户。

因此,我向适配器中的图像添加了一个点击监听器,就像这样

 void bind(Messages message) {

            String message_type = message.getType();
            String user_id = message.getFrom();
             String msg_id = message.getKey();

            if (message_type.equals("text")) {
                messageText.setText(message.getMessage());
                // Format the stored timestamp into a readable String using method.
                timeText.setText(DateUtils.formatDateTime(message.getTime()));

               // DatabaseReference messageRef = mRootRef.child("messages").child(mCurrentUserId).child(user_id).child()


                recieved_image.setVisibility(View.INVISIBLE);
                // nameText.setText(message.getSender().getNickname());

                // Insert the profile image from the URL into the ImageView.
                //  Utils.displayRoundImageFromUrl(mContext, message.getSender().getProfileUrl(), profileImage);
            }else {
                messageText.setVisibility(View.INVISIBLE);
                recieved_image.setVisibility(View.VISIBLE);
                Picasso.with(recieved_image.getContext()).load(message.getMessage()).into(recieved_image);
                recieved_image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(view.getContext(), MessageImageViewerActivity.class);
                        intent.putExtra("message_id",msg_id);
                        view.getContext().startActivity(intent);
                    }
                });

旨在将消息ID转移到下一个活动,该活动应用于从此类存储库中定位和加载图像

 msgImage = findViewById(R.id.MsgImageView);

    mAuth = FirebaseAuth.getInstance();
    final String message_id = getIntent().getStringExtra("message_id");


    mRootRef = FirebaseDatabase.getInstance().getReference();
    mCurrentUserId = mAuth.getCurrentUser().getUid();

    mImageStorage.child("message_images").child(message_id).getDownloadUrl().addOnCompleteListener(new OnCompleteListener<Uri>() {
        @Override
        public void onComplete(@NonNull Task<Uri> task) {
            if (task.isSuccessful()){
                String ima = task.getResult().toString();
               // Picasso.with(MessageImageViewerActivity.this).load(ima).into(msgImage);

                Glide.with(MessageImageViewerActivity.this)
                        .load(ima)
                        .into(msgImage);
            }
        }
    });

,但返回错误消息,表明路径无效。这是logcat错误消息。

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.storage.StorageReference com.google.firebase.storage.StorageReference.child(java.lang.String)' on a null object reference
    at com.mani.eric.quickchat.ui.MessageImageViewerActivity.onCreate(MessageImageViewerActivity.java:60)
    at android.app.Activity.performCreate(Activity.java:7327)
    at android.app.Activity.performCreate(Activity.java:7318)

请问我要怎么做,可能吗? 还是我做错了。

1 个答案:

答案 0 :(得分:1)

您需要执行以下操作:

StorageReference mImageStorage = FirebaseStorage.getInstance().getReference();

首先获取对Firebase存储的引用,然后访问您在Firebase存储控制台中创建的child

    mImageStorage.child("message_images").child(message_id).getDownloadUrl().addOnCompleteListener(new OnCompleteListener<Uri>() {
    @Override
    public void onComplete(@NonNull Task<Uri> task) {
        if (task.isSuccessful()){
            String ima = task.getResult().toString();
           // Picasso.with(MessageImageViewerActivity.this).load(ima).into(msgImage);

            Glide.with(MessageImageViewerActivity.this)
                    .load(ima)
                    .into(msgImage);
        }
    }
});