如何使用存储在firebase数据库中的url下载文件?

时间:2017-09-26 11:22:16

标签: android firebase firebase-realtime-database

我已将存储在firebase存储中的pdf文件的url存储在节点Pdf内的数据库中。我试图在存储在数据库中的url的帮助下从firebase存储下载文件。

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搜索文件,但我不确定是否这是搜索文件的正确方法。感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

正如您在调试时发现的那样,文件未下载,因为引用不正确。

您如何从用户那里获取有关要下载哪个文件的信息?

我相信您必须向他显示他/她上传的文件列表。因此,在该列表中,您可以存储有关file_name的信息。现在,当用户单击列表项时,您可以拥有正确的引用,因为您知道了单击的列表项中的file_name。

答案 1 :(得分:0)

在同样的事情中,我给你一个不同的方法,当我得到同样的错误时我使用。 实际上我将文件保存在Firebase Storage中,文件名与post_id相关,然后给出以下代码 看看

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

根据您的需要进行一些修改,这种方法可以像我一样工作....

答案 2 :(得分:0)

我自己解决了这个问题。 我使用StorageReference island=storage.getReferenceFromUrl(url);代替StorageReference island=storageRef.child("books").child(root_child).child(url);。这解决了我的问题。