Skip to content Skip to sidebar Skip to footer

Make Upload Method Return String "the Uploaded File Url"

I'm uploading an image to Firebase Storage, I need the image URL in the Storage to use it then to insert it within a document in FireStore. How to get the url after the upload proc

Solution 1:

Ref :: Image not retrieving from Firebase

//add file on Firebase and got Download Link
filePath.putFile(imageUri).continueWithTask(newContinuation<UploadTask.TaskSnapshot, Task<Uri>>() {
    @Overridepublic Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task)throws Exception {
        if (!task.isSuccessful()){
            throw task.getException();
        }
        return filePath.getDownloadUrl();
    }
}).addOnCompleteListener(newOnCompleteListener<Uri>() {
    @OverridepublicvoidonComplete(@NonNull Task<Uri> task) {
        if (task.isSuccessful()){
            UridownUri= task.getResult();
            Log.d(TAG, "onComplete: Url: "+ downUri.toString());
        }
    }
});

Solution 2:

we can get the URL of a file from taskSnapshot, like that:

public String uploadImage(byte[] bytes) {
    try {
        finalStorageReferenceref= storage.child("images/" + newDate().toString());
        ref.putBytes(bytes)
                .addOnSuccessListener(newOnSuccessListener<UploadTask.TaskSnapshot>() {
                    @OverridepublicvoidonSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        Task<Uri> urlTask = taskSnapshot.getStorage().getDownloadUrl();
                        while (!urlTask.isSuccessful()) ;
                        UridownloadUrl= urlTask.getResult();
                    }
                });
        System.out.println("RES : " + res);
        return res;
    }catch (NullPointerException e){
        returnnull;
    }
}

Hope it helps you!

Post a Comment for "Make Upload Method Return String "the Uploaded File Url""