Home > OS >  Upload a file received from there into Firebase Storage
Upload a file received from there into Firebase Storage

Time:05-09

In my code, I get a file from Firebase Storage and try to upload it there as well.

mStorageRef.child("write_but2.jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            mStorageRef.child("write_but3.jpg").putFile(uri);
        }
    });

Does not work:

E/UploadTask: could not locate file for uploading:https://firebasestorage...
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
No content provider: https://firebasestorage...
java.io.FileNotFoundException: No content provider: https://firebasestorage...

Please tell me what should I do?

CodePudding user response:

Make Sure Firebase reference path is correct

// Create a storage reference from our app
StorageReference storageRef = storage.getReference();

// Create a reference with an initial file path and name
StorageReference pathReference = storageRef.child("images/stars.jpg");

pathReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            // Got the download URL for 'images/stars.jpg'
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle any errors
        }
    });

CodePudding user response:

That's not how the documentation recommends uploading a file and reading the download URL. To achieve that, please use the following lines of code:

StorageReference ref = storageRef.child("images/write_but2.jpg");
Task uploadTask = ref.putFile(file);

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();
        }

        return ref.getDownloadUrl();
    }
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
    @Override
    public void onComplete(@NonNull Task<Uri> task) {
        if (task.isSuccessful()) {
            Uri downloadUri = task.getResult();
            //Do what you need to do with the URL.
        } else {
            Log.d(TAG, task.getException().getMessage()); //Never ignore potential errors!
        }
    }
});

  • Related