Firebase下载时的存储异常

3

我一直在搜索这个问题,但是在这里没有找到任何解决方案。我的问题是,当我将新项目发布到Firebase存储时,一切都运行良好,但是当我尝试下载它时,目录文件夹成功创建,但是文件未能下载,并显示以下错误异常:

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

        }
    });

当我点击按钮时,应该下载文件,但只创建了一个目录。

似乎在指定的路径中该对象不存在,请尝试使用我给出的答案。 - Ramana V V K
路径不对,代码是正确的,我太分心了,谢谢! - Lauris Macke
2个回答

3

这是一个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在photos文件夹中,则下载路径为photos/image.jpg。

1

检查Firebase规则..!

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接