将从那里收到的文件上传到 Firebase Storage

Upload a file received from there into Firebase Storage

在我的代码中,我从 Firebase 存储中获取了一个文件,并尝试将其上传到那里。

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

无效:

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...

请告诉我应该怎么做?

确保 Firebase 引用路径正确

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

这不是 documentation 推荐的上传文件和阅读下载 URL 的方式。为此,请使用以下代码行:

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