2017-05-25 108 views
2

我一直在尋找這個問題,但我還沒有找到任何解決方案在這裏。我的問題是,當我發佈新的項目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(); 
       } 
      }); 

     } 
    }); 

當我按下按鈕,文件應該被下載,創建但只有目錄:對象不以 位置

我的代碼在這裏存在。

+0

看來對象在指定的路徑不存在,並嘗試通過我給出的答案。 –

+0

路徑錯了,代碼是對的,我很分心,謝謝! –

回答

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的文件夾,然後將downloadPath照片/ image.jpg的

1

檢查火力地堡規則..!

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