I am not able to getdownloadurl() from firebase. Please anybody help here’s my code and error. (I am using Fragment)

Total
1
Shares

HERE IS MY CODE:

 private void uploadFile() {
    //checking if file is available
    if (filePath != null) {
        //displaying progress dialog while image is uploading
        final ProgressDialog progressDialog = new ProgressDialog(getContext());
        progressDialog.setTitle("Uploading");
        progressDialog.show();

        //getting the storage reference
        StorageReference sRef = storageReference.child(Constants.STORAGE_PATH_UPLOADS + System.currentTimeMillis() + "."
                + getFileExtension(filePath));

        UploadTask uploadTask = storageReference.putFile(filePath);

        Task<Uri> urlTask = uploadTask.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 sRef.getDownloadUrl();
            }
        }).addOnCompleteListener(new OnCompleteListener<Uri>() {
            @Override
            public void onComplete(@NonNull Task<Uri> task) {
                if (task.isSuccessful()) {
                    Uri downloadUri = task.getResult();

                    Postmember postmember = new Postmember();
                    DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();

                    postmember.setType("iv");

                    String id = mDatabase.push().getKey();
                    mDatabase.child(id).setValue(postmember);


                } else {
                    // Handle failures
                    // ...
                }
            }
        });
    }

}

HERE IS MY ERROR:

E/StorageException: StorageException has occurred.
An unknown error occurred, please check the HTTP result code and inner exception for server response.
 Code: -13000 HttpResult: 0

E/StorageException: Cannot upload to getRoot. You should upload to a storage location such as .getReference(‘image.png’).putFile…
java.lang.IllegalArgumentException: Cannot upload to getRoot. You should upload to a storage location such as .getReference(‘image.png’).putFile…
at com.google.firebase.storage.UploadTask.run(UploadTask.java:213)
at com.google.firebase.storage.StorageTask.lambda$getRunnable$7$StorageTask(StorageTask.java:1072)
at com.google.firebase.storage.-$$Lambda$StorageTask$q9YBoR_A8LB-JxTCx8JRQvabaZs.run(Unknown Source:2)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)


Solution

The following error:

E/StorageException: Cannot upload to getRoot. You should upload to a storage location such as .getReference(‘image.png’).putFile

Says very clear what the problem is. There is no way you can upload a file directly to the root directory without a name and a file extension. So the line that is causing you trouble is:

UploadTask uploadTask = storageReference.putFile(filePath);

Without seeing how the storageReference object is defined, I can say that most likely it points to the root directory. To solve the problem, you should change the above line of code to:

StorageReference sRef = storageReference.child(Constants.STORAGE_PATH_UPLOADS + System.currentTimeMillis() + "."
            + getFileExtension(filePath));
UploadTask uploadTask = sRef.putFile(filePath);

So the correct object that should be used is sRef, as it points to the right path and not storageReference.

Leave a Reply

Your email address will not be published. Required fields are marked *