繁体   English   中英

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

[英]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...

请告诉我该怎么办?

这不是文档推荐的上传文件和阅读下载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!
        }
    }
});

确保 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
        }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM