Androd view pdf file

When you create an Android application to download a PDF file from internet, you might want to view the file after download completed. In the tip download progress dialog, I showed you how to download a file from internet and display a progress dialog to show the percentage of downloading task. In this tip, I will show you how to view or open the PDF file stored in your phone's external storage.


After you create a new Android project, add onStart method to the MainActivity class. In the onStart method, you will write the following code to open the PDF file. You need to change path of the file to reflect a PDF file in your phone's external storage. In my case, i open a PDF file called Moi201.pdf stored in the edoc_download directory of the external storage.


public void onStart(){

super.onStart();
String path=Environment.getExternalStorageDirectory()+"/edoc_download/Moi201.pdf";
File file = new File(path);
   Intent target = new Intent(Intent.ACTION_VIEW);
   target.setDataAndType(Uri.fromFile(file),"application/pdf");
   target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
   Intent intent = Intent.createChooser(target, "Open File");
   try {
      startActivity(intent);
   } catch (ActivityNotFoundException e) {
      // Instruct the user to install a PDF reader here, or something
   }  

}


From the code above, to open the PDF file, first you need to create a File object to point to the file stored in your phone's external storage. After that, create an Intent object with Intent.ACTION_VIEW passed to its constructor. You will specify the file to open and its mime type by using the setDataAndType method. Then, you can use createChooser method to create a ACTION_CHOOSER intent to wrap the target intent. Finally use the the startActivity method to start the ACTION_CHOOSER intent. It will show a popup list of eligible applications to open the PDF file.


Androd-view-pdf-file

Posted by: Dara | post date: 05-31-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.