AliDeV
AliDeV

Reputation: 213

I want my app to open pdf

my app downlods a pdf file from a link saving it to SD card, and it's working fine.. but what I'm struggeling at is I couldn't let my app views the pdf file..( although I've installed PDF viewer on my emulator)nothing happens after downloading the file... here's my code for openning the pdf file

                                   InputStream input = new BufferedInputStream(url.openStream());
            File sdCard = Environment.getExternalStorageDirectory();
            File dir = new File(sdCard.getAbsolutePath()
                    + "/myapp/download");
            dir.mkdirs();
            File file = new File(dir, "application/pdf");
            FileOutputStream output = new FileOutputStream(file);

Upvotes: 1

Views: 719

Answers (1)

Yugandhar Babu
Yugandhar Babu

Reputation: 10349

Use below code to open pdf file.

File pdfFile = new File("/mnt/sdcard/path_to_file/yourpdf.pdf"); 
    if(pdfFile.exists()) {
        Uri path = Uri.fromFile(pdfFile); 
        Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
        pdfIntent.setDataAndType(path, "application/pdf");
        pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        try {
           startActivity(pdfIntent);
        } catch(ActivityNotFoundException e) {
           Toast.makeText(yourActivityClass.this, "No Application available to view pdf", Toast.LENGTH_LONG).show(); 
        }
    } else {
        Toast.makeText(yourActivityClass.this, "File not found", Toast.LENGTH_LONG).show(); 
    }

yourActivityClass.this is application context make it correct while testing above code.

Upvotes: 2

Related Questions