Firebase StorageException当下载
问题描述:
我一直在寻找这个问题,但我还没有找到任何解决方案在这里。我的问题是,当我发布新的项目firebase存储一切运作良好,但是当我尝试下载它,目录文件夹创建成功,但文件不下载,因为它显示我这个错误异常:Firebase StorageException当下载
com .google.firebase.storage.StorageException:
@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(); } }); } });
当我按下按钮,文件应该被下载,创建但只有目录:对象不以 位置
我的代码在这里存在。
答
这是firebase下载和检查下载路径的工作示例,对象应该存在于存储桶中。
// 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的文件夹,然后将downloadPath照片/ image.jpg的
答
检查火力地堡规则..!
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
private FirebaseAuth mAuth;
private FirebaseDatabase mdatabase;
private DatabaseReference mdatabaseReference;
StorageReference mFStorage;
StorageReference filePath;
mAuth = FirebaseAuth.getInstance();
mdatabase = FirebaseDatabase.getInstance();
mdatabaseReference = mdatabase.getReference();
mFStorage= FirebaseStorage.getInstance().getReference();
filePath=mFStorage.child("audio").child(audioId+".mp3");
filePath.putFile(imageGalleryUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
dialog.dismiss();
DownloadUrl=taskSnapshot.getDownloadUrl();
Log.d(TAG,"DownloadUrl.toString()");
//download link for file
}
});
然后使用下载管理器下载文件像音频
Context ctx=MainActivty.this;
DownloadManager downloadManager = (DownloadManager)ctx.getSystemService(DOWNLOAD_SERVICE);
//Your Url here
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDescription("Downloading a file");
long id = downloadManager.enqueue(request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle("File Downloading...")
.setDescription("Audio File Downloading...!")
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "/Audio/"+audioName+".mp3"));
看来对象在指定的路径不存在,并尝试通过我给出的答案。 –
路径错了,代码是对的,我很分心,谢谢! –