首页 > 解决方案 > 如何将文件从 Firebase 存储 url 下载到内部存储

问题描述

我想从 URL 下载 pdf 文件,我无法下载它显示不支持的路径。如何设置正确的路径。

 public void downloadAndOpenPdf(String url){

        String fileName = url.substring(url.lastIndexOf('/')+1, url.length());
        file = new File(Environment.getDataDirectory(), fileName);

            if(!file.isFile()) {
                DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                DownloadManager.Request req = new DownloadManager.Request(Uri.parse(url));
                req.setDestinationUri(Uri.fromFile(file));
               // req.setTitle("Some title");

                BroadcastReceiver receiver = new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        unregisterReceiver(this);
                        if (file.exists()) {
                            openPdfDocument(file);
                        }
                    }
                };
                registerReceiver(receiver, new IntentFilter(
                        DownloadManager.ACTION_DOWNLOAD_COMPLETE));
                dm.enqueue(req);
                Toast.makeText(this, "Download started", Toast.LENGTH_SHORT).show();
            }
            else {
                openPdfDocument(file);
            }
        }

        public boolean openPdfDocument(File file) {
            Intent target = new Intent(Intent.ACTION_VIEW);
            target.setDataAndType(Uri.fromFile(file), "application/pdf");
            target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            try {
                startActivity(target);
                return true;
            } catch (ActivityNotFoundException e) {
                Toast.makeText(this,"No PDF reader found",Toast.LENGTH_LONG).show();
                return false;
            }

        }

标签: androidfirebaseandroid-download-manager

解决方案


如果您要从 Firebase 存储下载文件,请尝试使用此代码

private void getFileUrl(){
StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("your file name as is from firebase storage");
    storageReference.getDownloadUrl().addOnSuccessListener(uri -> {
        String url = uri.toString();
        downloadFile(getContext(), "your file name", "file extension", DIRECTORY_DOWNLOADS, url);
        mProgressBar.hide();
    }).addOnFailureListener(e -> {
        mProgressBar.hide();
        ToastUtil.toastLong(getContext(), "something went wrong " + e.getMessage());
    });
}


private void 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);

    downloadmanager.enqueue(request);
}

让我知道它是否有效。


推荐阅读