0

whenever i use this method to retrieve stored image url in firebase storage taskSnapshot.getMetadata().getReference().getDownloadUrl().toString();
I expect that this method will return the download url of saved image, but what it returns is something with this format com.google.android.gms.tasks.zzu@41f55k88 here is firebase database tree enter image description here here is my code snippet.

final StorageReference filePath = userProfileImageRef.child(currentUserID + ".jpg");
            filePath.putFile(resultUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    final String downloadUrl = taskSnapshot.getMetadata().getReference().getDownloadUrl().toString();
                    Log.d("url", downloadUrl);
                    usersRef.child("profileimage").setValue(downloadUrl).addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if (task.isSuccessful()) {
                                Toast.makeText(SetupActivity.this, "Image stored", Toast.LENGTH_SHORT).show();
                            } else {
                                String message = task.getException().getMessage();
                                Toast.makeText(SetupActivity.this, "Error: " + message, Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            });
M.Moustafa
  • 31
  • 1
  • 7

2 Answers2

1

try to use this method

 filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {

                           final String downloadUrl = 
                           uri.toString();
                     }
                     });

so it will be

    final StorageReference filePath = userProfileImageRef.child(currentUserID + ".jpg");
                filePath.putFile(resultUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                      filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                            @Override
                            public void onSuccess(Uri uri) {

                               final String downloadUrl = 
                               uri.toString();
                         }
                         });
                    }


                        usersRef.child("profileimage").setValue(downloadUrl).addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                if (task.isSuccessful()) {
                                    Toast.makeText(SetupActivity.this, "Image stored", Toast.LENGTH_SHORT).show();
                                } else {
                                    String message = task.getException().getMessage();
                                    Toast.makeText(SetupActivity.this, "Error: " + message, Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                });
Momen Zaqout
  • 1,508
  • 1
  • 16
  • 17
0

To do what you want i would do something like this:

I am gonna assume you have a Uri with the image path lets call it: filepath then:

final String filename = filepath.getPath().substring(filepath.getPath().lastIndexOf("/")+1);
final StorageReference storagePath = mStorageRef.child("imagens/").child(filename);

storagePath.putFile(filepath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        storagePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                FileInfo fileInfo = new FileInfo(uri.toString(), filename);
                FirebaseDatabase.getInstance().getReference("The path in your database").push().setValue(fileInfo);
                Toast.makeText(UploadActivity.this, "Upload done", Toast.LENGTH_LONG).show();
            }
        });
    }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(UploadActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    });

The onSuccess method has the uri that you want, i am saving it in a class FileInfo.

After the upload is done and the information about the file uploaded is stored in the database you retrieve the information like this:

mFirebaseInstance = FirebaseDatabase.getInstance();
mDatabase = mFirebaseInstance.getReference("The path in your database");
mDatabase.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        filesList.clear();
        for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
            FileInfo fileInfo = dataSnapshot1.getValue(FileInfo.class);
                filesList.add(fileInfo);
        }
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {
        //Handle the mehtod in your way
    }
});

In the above code i am retrieving from the database all the "nodes" under the given path you give it, for the case you have more then 1 image that you would like to get the url and i am saving the content in my FileInfo class that looks like this:

import android.net.Uri;

import java.io.Serializable;

public class FileInfo implements Serializable{

    String filename, downloadURL;

    public FileInfo(){
    }

    public FileInfo(String downloadURL, String filename) {
        this.downloadURL = downloadURL;
        this.filename = filename;
    }

    public String getDownloadURL() {
        return downloadURL;
    }

    public void setDownloadURL(String downloadURL) {
        this.downloadURL = downloadURL;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }
}

Firebase using the Serialize implementation does the job of filling the class for you with the content that exist in each "node" of the database.

Note: you dont need the class but i think it fits your problem better since you save more than just the image url in the dabase that way you can adapt the class to your needs

MiguelD
  • 409
  • 1
  • 7
  • 16