Open PDF in android app

Since your catch block has ActivityNotFoundException, it means that you don't have any activity/application that can read a 'application/pdf' filetype format. Install any pdf viewer from the Android Market (Adobe recently release theirs), or else use the above mentioned open source pdf viewer and your problem will most probably will be solved.

http://code.google.com/p/apv/downloads/list

https://market.android.com/details?id=cx.hell.android.pdfview&feature=search_result

When you start activity with your given params, it searches for all applications/Activities/Intents that are registered to open pdf format. Since you have none in your device, you are getting the ActivityNotFoundException


First install the pdf reader on device. than use this code for reading pdf file from internal memory.

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    final TextView tv = (TextView)findViewById(R.id.tv);
    Button bt=(Button)findViewById(R.id.openbtn);
    bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

    File pdfFile = new File(Environment.getExternalStorageDirectory(),"Leave.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){
        tv.setText("No Application available to view PDF");
    }
    }
    else
    {
        tv.setText("File not found");
    }

        }
    });

}


Your code is right, I have also used same code to open pdf file within a viewer.

As you don't have a viewer installed to your device so it can't open without any viewer.

You can install Adobe reader for Android.

I can't open pdf file within emulator, so I have to test using my device.

Tags:

Pdf

Android