Search code examples
androidpdffile-ioviewer

Opening a PDF File in Android


I am trying to open a link to a pdf file and display the pdf when a button is clicked on the device. What is the best way to go about this? I would like to be able to not use a 3rd party software. So I know that I might have to convert the file to something else.


Solution

  • Try this inside your Button's click listener:

    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file),”application/pdf”);
    intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    startActivity(intent);
    

    You should check if any PDF reading application is available before using this code.

    If you are planning to implement your own PDF reader then refer this.