Suhail Parvez
Suhail Parvez

Reputation: 198

Open a Downloaded file (pdf) from the Downloads folder in the INTERNAL STORAGE

I have downloaded a file (133465.pdf) using the Download manager and now it is stored in the Downloads folder of the mobile phone (Internal storage).

How should i try and retrieve the downloaded pdf from the Downloads folder?

I am using the below code to try and retrieve the pdf from the downloads folder but i am getting an error on the Toast, saying "Cannot display PDF (133465.pdf cannot be opened)" .

String file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() +  File.separator + "133465.pdf";
                Log.i("Fragmentadapter1", file);
                File videoFile2Play = new File(file);
                Intent i = new Intent();
                i.setAction(android.content.Intent.ACTION_VIEW);
                i.setDataAndType(Uri.fromFile(videoFile2Play), "application/pdf");
                imageContext.startActivity(i);

I don't know if i am using the right file location to access the file.

Any help or suggestion will be appreciated.

Upvotes: 6

Views: 11655

Answers (1)

Suhail Parvez
Suhail Parvez

Reputation: 198

If you are working for Lollopop and Below: You don't have to ask the user for permission on run-time. The manifest Permissions will do.

If you are working for Marshmellow and up: You have to ask the user for permission on run-time and act according to the users output.

Remember: You still have to give the Permissions on the Manifest too.

To Download a PDF on users Downloads folder :

DownloadManager downloadmanager;
    Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
            .mkdirs();

    downloadmanager = (DownloadManager) getApplication().getSystemService(Context.DOWNLOAD_SERVICE);
    String url = hardcode + bb ;
    Uri uri = Uri.parse(url);
    DownloadManager.Request request = new DownloadManager.Request(uri)
            .setTitle(bb )
            .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                    bb)
            .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    Log.i("Download1", String.valueOf(request));
    downloadmanager.enqueue(request);

Reference

To View the Downloaded PDF from the Downloads folder of the Users device:

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + File.separator +
            "YOUR FILE NAME");
    Uri path = Uri.fromFile(file);
    Log.i("Fragment2", String.valueOf(path));
    Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
    pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    pdfOpenintent.setDataAndType(path, "application/pdf");
    try {
        this.startActivity(pdfOpenintent);
    } catch (ActivityNotFoundException e) {

    }

Note: Make sure u get the permission granted before downloading the Viewing the PDF file for Marshmellow and UP.

Upvotes: 8

Related Questions