【问题标题】:How to download pdf File from Firebase storage using download link in android development如何使用android开发中的下载链接从Firebase存储下载pdf文件
【发布时间】:2018-06-06 10:18:52
【问题描述】:

我在存储文件时获得了下载链接,然后将其保存在数据库中。现在我想创建一个listView,当用户单击下载按钮时,该文件保存在移动内部/外部存储中时有下载按钮。怎么做?

这是我的数据库屏幕截图。

【问题讨论】:

    标签: android firebase firebase-realtime-database firebase-storage


    【解决方案1】:

    您可以使用内置下载管理器下载:只需使用适当的参数调用此函数即可开始下载,您还可以在通知托盘中查看状态。

    public long downloadFile(Context context, String fileName, String fileExtension, String destinationDirectory, String url) {
    
    
         DownloadManager downloadmanager = (DownloadManager) context.
                getSystemService(Context.DOWNLOAD_SERVICE);
         Uri uri = Uri.parse(url);
         DownloadManager.Request request = new DownloadManager.Request(uri);
    
         request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
         request.setDestinationInExternalFilesDir(context, destinationDirectory, fileName + fileExtension);
    
         return downloadmanager.enqueue(request);
    }
    

    【讨论】:

    • P.S.:您还需要实现一个侦听器以接收诸如 DOWNLOAD FINISHED 或 FAILED 等回调。
    【解决方案2】:
    //create this Async task class to download file 
    class DownloadFileFromURL extends AsyncTask<String, String, String> {
    
            /**
             * Downloading file in background thread
             * */
            @Override
            protected String doInBackground(String... f_url) {
                int count;
                try {
                    URL url = new URL(f_url[0]);
                    URLConnection conection = url.openConnection();
                    conection.connect();
    
                    // this will be useful so that you can show a tipical 0-100%
                    // progress bar
                    int lenghtOfFile = conection.getContentLength();
    
                    // download the file
                    InputStream input = new BufferedInputStream(url.openStream(),
                            8192);
    
                    // Output stream
                    OutputStream output = new FileOutputStream(Environment
                            .getExternalStorageDirectory().toString()
                            + f_url[1]);
    
                    byte data[] = new byte[1024];
    
                    long total = 0;
    
                    while ((count = input.read(data)) != -1) {
                        total += count;
                        // writing data to file
                        output.write(data, 0, count);
                    }
    
                    // flushing output
                    output.flush();
    
                    // closing streams
                    output.close();
                    input.close();
    
                } catch (Exception e) {
                    Log.e("Error: ", e.getMessage());
                }
    
                return null;
            }
    
        }
        //call this async task class from somewhere like
        new DownloadFileFromURL().execute(file_url,"/test.pdf");
    

    【讨论】:

      【解决方案3】:
       ChildEventListener childEventListener = new ChildEventListener() {
       @Override
       public void onChildAdded(DataSnapshot dataSnapshot, String 
         previousChildName) {
           String fileName = dataSnapshot.getKey();
           String downloadUrl = dataSnapshot.getValue(String.class);
           // Add pdf to the display list.
           // displayList contains urls of pdfs to be downloaded.
           displayList.add(downloadUrl);
        }
        // Other methods of ChildEventListener go here
      };
      pdfRef.addChildEventListener(childEventListener);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-23
        • 2018-01-08
        • 2021-07-15
        • 1970-01-01
        • 1970-01-01
        • 2020-03-14
        • 2021-01-26
        • 2016-11-01
        相关资源
        最近更新 更多