简体   繁体   中英

Load image from firebase storage into image view

In my app users are able to send text and images to other users and now i also want when a user clicks on the sent or received image they will be taken to another activity were the image will be loaded into an image view for the user.

So i added a click listener to the image in my adapter like this

 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);
                    }
                });

with the aim of transferring the message id to the next activity which should be used to locate and load the image from firebase storage like this

 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);
            }
        }
    });

but it is returning an error that the path is invalid. this is the logcat error message.

 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)

please what I'm trying to do, is it possible? or what I'm i not doing right.

You need to do the following:

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

First get the reference to the firebase storage, then access the child that you created in the firebase storage console.

    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);
        }
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM