0

So , I am working on small chat application that can load images stored in Firebase cloud.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_SIGN_IN) {
        // Code for text message
    }
        else if(requestCode==RC_PHOTO_PICKER && resultCode==RESULT_OK){
            Uri selectedImageUri=data.getData();
            StorageReference photoRef=mChatPhotoReference.child(selectedImageUri.getLastPathSegment());
            photoRef.putFile(selectedImageUri).addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    String downloadUrl=taskSnapshot.getMetadata().getReference().getDownloadUrl().toString();
                    FriendlyMessage friendlyMessage=new FriendlyMessage(null,mUsername,downloadUrl);
                    mDatabaseReference.push().setValue(friendlyMessage);
                    Log.e("WWWWWWWWWWWWWWWWWWWWWWW",downloadUrl);

                }
            });
    }

}

and here is get view .

public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.item_message, parent, false);
    }

    ImageView photoImageView = (ImageView) convertView.findViewById(R.id.photoImageView);
    TextView messageTextView = (TextView) convertView.findViewById(R.id.messageTextView);
    TextView authorTextView = (TextView) convertView.findViewById(R.id.nameTextView);

    FriendlyMessage message = getItem(position); 

//FriendlyMessage is class that stores text , username and image url

    boolean isPhoto = message.getPhotoUrl() != null;
    if (isPhoto) {
        Log.e("VVVVVVVVVVVVVVVVVVVV",message.getPhotoUrl());
        messageTextView.setVisibility(View.GONE);
        photoImageView.setVisibility(View.VISIBLE);
        Glide.with(photoImageView.getContext())
                .load(message.getPhotoUrl())
                .into(photoImageView);
    } else {
          Load text..
    }
    authorTextView.setText(message.getName());

    return convertView;
}

I a am using

glidle : 4.11.0 firebase-storage:19.1.0

AmanSharma
  • 821
  • 9
  • 15
  • Please check if you're receiving `message.getPhotoUrl()` value inside – Ashish Jan 24 '20 at 12:27
  • @Ashish Yes , I looged it and getting correct value. – AmanSharma Jan 24 '20 at 12:31
  • @AmanSharma try to open that in browser – Rahul sharma Jan 24 '20 at 12:33
  • @frankenstein , they are unlike webadresses , they are specific references inside cloud storage example : com.google.android.gms.tasks.zzu@d4cf139 – AmanSharma Jan 24 '20 at 12:35
  • 1
    Does this answer your question? [How to get the download url from Firebase Storage?](https://stackoverflow.com/questions/53299915/how-to-get-the-download-url-from-firebase-storage) – jbmcle Jan 24 '20 at 12:40
  • @AmanSharma That is a java object – jbmcle Jan 24 '20 at 12:42
  • 1
    @AmanSharma that's why glide not able to access. You need url like this `https://firebasestorage.googleapis.com/v0/b/xxxxApp.appspot.com/o/logo.png?alt=media&token=c0472xxxxxxxxx88c44` .And it required `Token`. – Rahul sharma Jan 24 '20 at 12:44
  • thanx @frankenstein , Indeed the Url which i was using was actually java object as pointed out by _jake , However after using the referred Post , i was able to store correct URL it. – AmanSharma Jan 24 '20 at 12:53
  • @jake , yeah that post did work . – AmanSharma Jan 24 '20 at 12:55

2 Answers2

1

Try with RequestOptions all should looks like

RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.mipmap.ic_launcher_round)
                    .error(R.mipmap.ic_launcher_round);



 Glide.with(this)
    .load(image_url)
    .apply(options)
    .into(imageView);

If you doesn't get url you will display error images, in this case ic_lanucher_round

KyluAce
  • 933
  • 1
  • 8
  • 25
  • I did that and icon is not displayed . However i logged urls and they are of form com.google.android.gms.tasks.zzu@d4cf1xx – AmanSharma Jan 24 '20 at 12:36
  • try to change inside get view to something like that `if (convertView == null) { final LayoutInflater layoutInflater = LayoutInflater.from(context); convertView = layoutInflater.inflate(R.layout.item_message, false); }` – KyluAce Jan 24 '20 at 12:43
  • 1
    solved it , the problem was : taskSnapshot.getMetadata().getReference().getDownloadUrl() returned task object instead of Uri @kyluAce – AmanSharma Jan 24 '20 at 12:56
1

The url you are receiving doesnt contain image, its still uploading there. You need to do something like this. Here while loop will make it wait, until image is uploaded completely.

 storageRef.child(UUID.randomUUID().toString()).putFile(uri)
                .addOnSuccessListener { p0 ->
                    val downloadUrl = p0!!.storage.downloadUrl
                    @Suppress("ControlFlowWithEmptyBody")
                    while (!downloadUrl.isSuccessful);

                    try {
                        val imagePath = downloadUrl.result!!.toString()
}

its in Kotlin, you can translate easily.

Haider Saleem
  • 773
  • 1
  • 9
  • 17