1

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);
AliDeV
  • 213
  • 2
  • 10
  • 1
    Search before you post, please. [Read This](http://stackoverflow.com/questions/2456344/display-pdf-within-app-on-android) and [This](http://stackoverflow.com/questions/1697726/how-to-open-pdf-and-read-it) – Calvin Mar 29 '12 at 02:21

1 Answers1

2

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.

Yugandhar Babu
  • 10,311
  • 9
  • 42
  • 67