代码之家  ›  专栏  ›  技术社区  ›  Imran Sk

android开发中如何使用下载链接从firebase存储下载pdf文件

  •  0
  • Imran Sk  · 技术社区  · 6 年前

    我得到下载链接时,我存储的文件,然后我保存在数据库。现在我想创建一个列表视图,当用户点击下载按钮时,这里有下载按钮,文件保存在移动存储中。怎么做?

    这是我的数据库屏幕短。

    enter image description here

    3 回复  |  直到 6 年前
        1
  •  2
  •   Yash Soni    6 年前

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

    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);
    }
    
        2
  •  0
  •   mahesh talaviya    6 年前
     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);
    
        3
  •  0
  •   Abdullah Tellioglu    6 年前
    //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");