简体   繁体   中英

Unable to retrieve image's download URL from Firebase Storage (getting exception) [NEED URGENT HELP]

The code successfully uploads an image to Firebase Storage but doesn't give back the download URL. How can I fix this?

I get this exception: java.lang.IllegalArgumentException: getDownloadUrl() is not supported at the root of the bucket. Why?

private void uploadFile() {
    if (mImageUri != null) {
        StorageReference fileReference = mStorageRef.child(System.currentTimeMillis()
                + "." + getFileExtension(mImageUri));

        fileReference.putFile(mImageUri).continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
            @Override
            public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                if (!task.isSuccessful()) {
                    throw task.getException();
                }

                // Continue with the task to get the download URL
                return mStorageRef.getDownloadUrl();
            }
        }).addOnCompleteListener(new OnCompleteListener<Uri>() {
            @Override
            public void onComplete(@NonNull Task<Uri> task) {
                if (task.isSuccessful()) {
                    Uri downloadUri = task.getResult();
                        System.out.println("Upload success: " + downloadUri);
                } else {
                    // Handle failures
                    // ...
                }
            }
        });

    } else {
        Toast.makeText(this, "No file selected", Toast.LENGTH_SHORT).show();
    }

}

I think there is problem in returning downloading url, try below code:

private void uploadFile() {
    if (mImageUri != null) {
        StorageReference fileReference = mStorageRef.child(System.currentTimeMillis()
                + "." + getFileExtension(mImageUri));

        fileReference.putFile(mImageUri).continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
            @Override
            public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                if (!task.isSuccessful()) {
                    throw task.getException();
                }

                // Continue with the task to get the download URL
                //change made here
                return fileReference.getDownloadUrl();
            }
        }).addOnCompleteListener(new OnCompleteListener<Uri>() {
            @Override
            public void onComplete(@NonNull Task<Uri> task) {
                if (task.isSuccessful()) {
                    Uri downloadUri = task.getResult();
                        System.out.println("Upload success: " + downloadUri);
                } else {
                    // Handle failures
                    // ...
                }
            }
        });

    } else {
        Toast.makeText(this, "No file selected", Toast.LENGTH_SHORT).show();
    }

}

So I have the same problem. I had a code that I´ve been using for two years but something seems to have changed in Firebase. Bellow is a new code that works and solve this problems. Ps: It´s in kotlin but you can have android sample in reference link and just adjust to my code.

first declare a global variable private lateinit var filePath: Uri

Then place your image inside this uri file.

then go to the code where you want to make the upload:

    mFireBaseStorage = FirebaseStorage.getInstance()
    mphotoStorageReference = mFireBaseStorage.reference  //storage references

    val storageRef = mphotoStorageReference.child("usuarios_img")  //path in storage. This is the folder name in storage

    val bmp: Bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath)
    val baos: ByteArrayOutputStream = ByteArrayOutputStream()
    bmp.compress(Bitmap.CompressFormat.JPEG, 25, baos) //choose compreess rate (100 means no compression)

    //get the uri from the bitmap
    val tempUri: Uri = getImageUri(this, bmp)
    //transform the new compressed bmp in filepath uri
    filePath = tempUri  //filePath is a global variable Uri


   
   var uploadTask = storageRef.child("the_name_of_the_file).putFile(filePath)

    // [START upload_get_download_url]
    val ref = storageRef
    uploadTask = ref.putFile(filePath)

    val urlTask = uploadTask.continueWithTask { task ->
        if (!task.isSuccessful) {
            task.exception?.let {
                throw it
            }
        }
        ref.downloadUrl
    }.addOnCompleteListener { task ->
        if (task.isSuccessful) {
            val downloadUri = task.result
            Log.d("test", "worked, this is the url link "+downloadUri)
            
           
        } else {
            // Handle failures
            Log.d("test", "error")

        }
    }

Thats all.

Reference https://firebase.google.com/docs/storage/android/upload-files?hl=pt-br

Hope it helps because I've lost some hours in it.

I have solved the solution for this. But I'm using Kotlin. I hope this will helps the others developer. I put some explanation about it.

    storageReference.child(Constant.EDUCATION).child(id).putFile(dataUpload!!.data!!)
        .addOnProgressListener {
            handleOnProgressUpload(it)
        }.addOnSuccessListener { task ->
            //At here onSuccess, we get the task to get the downloadUrl
            task.storage.downloadUrl.addOnSuccessListener {
                val downloadUrl = it.toString()
                handleSuccessDownloadUrl(downloadUrl)
            }
        }
<style name="Widget.Material.TextView.ListSeparator" parent="Widget.TextView.ListSeparator">
    <item name="background">@drawable/list_section_divider_material</item>
    <item name="textAllCaps">true</item>

 - </style>

<style name="Widget.Material.Light.TextView.ListSeparator" parent="Widget.Material.TextView.ListSeparator"/>

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