【问题标题】:Firebase StorageException when download下载时出现 Firebase StorageException
【发布时间】:2017-10-25 23:07:41
【问题描述】:

我一直在寻找这个问题,但在这里我没有找到任何解决方案。我的问题是,当我将新项目发布到 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();
                }
            });

        }
    });

当我按下按钮时,应该下载文件,但是只创建目录。

【问题讨论】:

  • 指定路径中好像不存在该对象,试试我给出的答案。
  • 路径错了,代码是对的,我心烦意乱,谢谢!

标签: android firebase download firebase-storage


【解决方案1】:

这是 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 文件夹中,然后是 downloadPath photos/image.jpg

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2019-10-05
      • 2015-09-16
      • 2018-05-30
      • 2016-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 2020-07-21
      相关资源
      最近更新 更多