David Brown
David Brown

Reputation: 4823

Open PDF in android app

I am working on an application where need to open the pdf file in the device,

I have actually got the code on the web that is similar to most of the examples. But, the thing is that I am not able to open the file and the control goes to the "Exception" part directly.

Here is the code below:

public class MyPDFDemo extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)   
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    Button OpenPDF = (Button) findViewById(R.id.button);
    OpenPDF.setOnClickListener(new View.OnClickListener()
    { 
        public void onClick(View v) 
        {
            File pdfFile = new File("/sdcard/Determine_RGB_Codes_With_Powerpoint [PDF Library].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(MyPDFDemo.this, "No Application available to view pdf", Toast.LENGTH_LONG).show(); 
                }
            }

        }
    });

}

When I run this code : i used to see " No Application available to view pdf ". Can anyone please me to view the pdf file.

Upvotes: 17

Views: 41588

Answers (3)

Kishor N R
Kishor N R

Reputation: 1591

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

        }
    });

}

Upvotes: 3

Yousuf Zaman
Yousuf Zaman

Reputation: 191

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.

Upvotes: 2

Ashok Goli
Ashok Goli

Reputation: 5183

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

Upvotes: 9

Related Questions