下载时出现Firebase StorageException [英] Firebase StorageException when download
问题描述
我一直在寻找这个问题,但是在这里我没有找到任何解决方案。我的问题是,当我将新项目发布到Firebase存储时,一切正常,但是当我尝试下载它时,目录文件夹创建成功,但是文件未下载,因为它显示了此错误异常:
I have been searching for this problem, but I have not found any solution here. My problem is that when I post new item to firebase storage everything works well, but when I try to download it, directory folder is created successfully, but file is not downloaded as it shows me this error exception:
com.google.firebase.storage.StorageException:对象在
位置不存在
com.google.firebase.storage.StorageException: Object does not exist at location
我的代码在这里:
@Override
public void onButtonDownloadClick(View view, final int position) {
String name = list.get(position).getRemoteName();
File storagePath = new File(Environment.getExternalStorageDirectory(), "FromFiles");
// Create direcorty if not exists
if(!storagePath.exists()) {
storagePath.mkdirs();
}
final File myFile = new File(storagePath, list.get(position).getRemoteName());
islandRef = storageReference.child(uid).child("files").child(name);
islandRef.getFile(myFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
// Local temp file has been created
Toast.makeText(getActivity(), "Succeed", Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
Toast.makeText(getActivity(), exception.toString(), Toast.LENGTH_LONG).show();
}
});
}
});
当我按下按钮时,应该下载文件,但是仅创建目录。
When I press button, file should be downloaded, however only directory is created.
推荐答案
这是firebase下载的工作示例,并检查下载路径,该对象应该存在于存储桶中。
This is working example for the firebase download and check the download path, the object should exist in the bucket.
// Initialize Storage
//storage
mStorage = FirebaseStorage.getInstance("gs://<bucket_name>");
mStorageRef = mStorage.getReference();
final StorageReference downloadRef;
downloadRef = mStorageRef.getRoot().child(downloadPath);
try {
File output = new File(Environment.getExternalStorageDirectory() + File.separator + Config.MY_VIDEOS_PATH);
if (!output.exists()) {
output.mkdir();
}
localFile = new File(output, downloadId);
} catch (Exception e) {
e.printStackTrace();
}
// Download and get total bytes
downloadRef.getFile(localFile)
.addOnProgressListener(new OnProgressListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onProgress(FileDownloadTask.TaskSnapshot taskSnapshot) {
showProgressNotification(1,title, "",
taskSnapshot.getBytesTransferred(),
taskSnapshot.getTotalByteCount());
}
})
.addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Log.d(TAG, "download:SUCCESS");
// Send success broadcast with number of bytes downloaded
broadcastDownloadFinished(downloadPath, taskSnapshot.getTotalByteCount());
showDownloadFinishedNotification(downloadPath, (int) taskSnapshot.getTotalByteCount());
// Mark task completed
taskCompleted();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Log.w(TAG, "download:FAILURE", exception);
Log.w(TAG, "download:FAILURE", exception.getCause());
// Send failure broadcast
broadcastDownloadFinished(downloadPath, -1);
showDownloadFinishedNotification(downloadPath, -1);
// Mark task completed
taskCompleted();
}
});
让我们假设您的image.jpg在photos文件夹中,然后下载路径photos / image.jpg
Let us assume that your image.jpg in the photos folder then the downloadPath photos/image.jpg
这篇关于下载时出现Firebase StorageException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!