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



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

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

相关内容

  • 没有找到相关文章

最新更新