Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open PDF in android app

Tags:

android

pdf

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.

like image 849
David Brown Avatar asked May 21 '11 05:05

David Brown


People also ask

Can Android phones open PDF files?

Adobe Reader is quite popular on Windows PCs, and it is one of the best Android PDF apps available on the Google Play Store. The app is easy to use, and it lets you open PDF on Android within seconds.

Why PDF is not opening in Android?

To fix a PDF file not opening in Adobe reader, you will need to download the latest version of Adobe Reader. After which you will disable the protected mode that comes with it by default. Once this is changed, the issue of the PDF file not opening in Adobe reader will be resolved.


3 Answers

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

like image 94
Ashok Goli Avatar answered Oct 23 '22 12:10

Ashok Goli


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

        }
    });

}

like image 3
Kishor N R Avatar answered Oct 23 '22 12:10

Kishor N R


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.

like image 2
Yousuf Zaman Avatar answered Oct 23 '22 12:10

Yousuf Zaman