2017-09-26 35 views
0

我已將存儲在firebase存儲中的pdf文件的url存儲在節點Pdf中的數據庫中。我試圖從存儲在數據庫中的url的幫助下從firebase存儲中下載文件。如何使用存儲在firebase數據庫中的url下載文件?

public class DownloadFile extends AppCompatActivity{ 
DatabaseReference databaseReference= FirebaseDatabase.getInstance().getReference(); 
String root_child="my book"; 
@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
databaseReference.child("Pdf").addValueEventListener(new ValueEventListener() { 
@Override 
public void onDataChange(DataSnapshot dataSnapshot) { 
if(dataSnapshot.hasChild(root_child)) 
{ 

String url=dataSnapshot.child(root_child).getValue().toString(); 
StorageReference island=storageRef.child("books").child(root_child).child(url); 
island.getFile(file).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() { 
@Override 
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) { 
pdfView.fromFile(file).load(); 
} 
}); 
} 

} 

的問題是沒有真正體現出了pdf視圖中。當我調試應用程序時,我發現StorageReference island=storageRef.child("books").child(root_child).child(url);未創建正確的參考。簡而言之,該文件不會下載。由於文件名稱因用戶上傳而不同,因此我無法指定文件名,因此我使用'child(url)',以便它可以使用url搜索文件,但我不確定是否正確搜索文件file.Any幫助表示讚賞。

+0

請發佈正確的代碼以方便閱讀。 –

+1

您是否參考https://firebase.google.com/docs/storage/android/download-files – Vij

回答

0

我自己得到了問題的解決方案。我使用StorageReference island=storage.getReferenceFromUrl(url);而不是StorageReference island=storageRef.child("books").child(root_child).child(url);。這解決了我的問題。

0

正如您在調試過程中發現的那樣,文件不會被下載,因爲引用不正確。

您是如何從用戶那裏獲得有關要下載哪個文件的信息的?

我相信你必須向他展示他/她上傳的文件列表。因此,在該列表中,您可以存儲有關file_name的信息。現在,當用戶單擊列表項時,您可以擁有正確的引用,因爲您知道單擊的列表項中的file_name。

+0

用戶首先從手機上傳文件,然後上傳的文件應顯示在pdf視圖中。該文件被上傳到firebase存儲中,url存儲在數據庫中,但我無法使用相同的url下載它。 –

+0

如何決定要下載哪個文件?你是否顯示在列表視圖中上傳的所有文件列表,以便當用戶點擊其中一個項目時,它將被下載? –

0

在同樣的事情,我給你一個不同的方法,我使用時,我得到了同樣的錯誤。 其實我保存文件Firebase Storage交該文件相關的,然後給拍下面的代碼 給看看

    private void downloadFile(final String postKey) { 
        File cFile = new File(getContext().getCacheDir(), "app Directory"); 
        final File cLocalFile = new File(cFile, postKey); 
        if (!rootPath.exists()) { 
         rootPath.mkdirs(); 
        } 
        if (!cFile.exists()) { 
         cFile.mkdirs(); 
        } 
        final FileDownloadTask downloadingTask = mStorage.child(postKey).getFile(cLocalFile); 
        downloadingTaskProgress(downloadingTask, cLocalFile); 
       } 

       private void downloadingTaskProgress(FileDownloadTask downloadingTask, final File cLocalFile) { 
        downloadingTask.addOnProgressListener(new OnProgressListener<FileDownloadTask.TaskSnapshot>() { 
         @Override 
         public void onProgress(final FileDownloadTask.TaskSnapshot taskSnapshot) { 
          double progressSize = (100.0 * taskSnapshot.getBytesTransferred()) 
            /taskSnapshot.getTotalByteCount(); 

         } 
        }).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() { 
         @Override 
         public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) { 

          Toast.makeText(getActivity(), "File download complete: " + model.fileName, Toast.LENGTH_SHORT).show(); 

          try { 
           writeToStorage(cLocalFile); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
         } 
        }).addOnFailureListener(new OnFailureListener() { 
         @Override 
         public void onFailure(@NonNull Exception exception) { 

          Toast.makeText(getContext(), "local file not created: " + 
            exception.toString(), Toast.LENGTH_LONG).show(); 
         } 
        }); 
       } 

       private void writeToStorage(File src) throws IOException { 
        FileChannel inChannel = new FileInputStream(src).getChannel(); 
        FileChannel outChannel = new FileOutputStream(localFile).getChannel(); 
        try { 
         inChannel.transferTo(0, inChannel.size(), outChannel); 
        } finally { 
         if (inChannel != null) 
          inChannel.close(); 
         if (outChannel != null) 
          outChannel.close(); 
        } 
       } 

給予一定的修改,按您的需要,這種方法可以工作作爲POST_ID名它在我的...

+0

thnx您的建議。我會試試這個。 –

+0

試一試,讓我知道如果工作@digital_pro –

相關問題