Как загрузить файл с URL-адреса хранилища Firebase во внутреннее хранилище - PullRequest
0 голосов
/ 24 апреля 2019

Я хочу скачать PDF-файл с URL-адреса, я не могу его скачать, показывает Неподдерживаемый путь. как установить правильный путь.

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

        }

1 Ответ

0 голосов
/ 24 апреля 2019

, если вы загружаете файлы из хранилища 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);
}

дайте мне знать, если это работает.

...