2017-09-26 32 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 –

相关问题