Sunday, 9 April 2017

View PDF In Android - Example

In this Example we will see how to view a pdf file through an android app. Here we will open the pdf file saved in the SD card with an application to view the pdf file like the Adobe Reader , Quick Office or any other application capable of reading pdf files.

In this application on tapping a button we check for the existence of the pdf file in the SD card. If the file exists we call the reader application via Intents. In the due course if the reader application is not found it is notified by catching the exception ActivityNotFoundException.


So lets start:

1.Create an Android Application Project with any Project Name and Package Name. Create an Activity and Layout file for the same like MainActivity & activity_main.

2.Design the screen for the activity with a button as below: 

activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
02    xmlns:tools="http://schemas.android.com/tools"
03    android:layout_width="fill_parent"
04    android:layout_height="fill_parent"
05    android:background="#FFFFFF"
06    android:paddingBottom="@dimen/activity_vertical_margin"
07    android:paddingLeft="@dimen/activity_horizontal_margin"
08    android:paddingRight="@dimen/activity_horizontal_margin"
09    android:paddingTop="@dimen/activity_vertical_margin"
10    tools:context=".Vibrator" >
11
12    <TextView
13        android:id="@+id/textView1"
14        android:layout_width="wrap_content"
15        android:layout_height="wrap_content"
16        android:layout_alignParentTop="true"
17        android:layout_centerHorizontal="true"
18        android:layout_marginTop="124dp"
19        android:text="Welcome to AndroidBite !!!"
20        android:textColor="@android:color/black"
21        android:textSize="20sp" />
22
23    <Button
24        android:id="@+id/bPressMe"
25        android:layout_width="wrap_content"
26        android:layout_height="wrap_content"
27        android:layout_centerHorizontal="true"
28        android:layout_centerVertical="true"
29        android:text="Press Me !!!" />
30
31</RelativeLayout>

3.Now in the activity, we create a File instance and construct it using specified directory and name. Then we set the Uri from the file instance and start the intent Intent.ACTION_VIEW . This intents data is set with the uri and the MIME Type for pdf is set as application/pdf before calling the intent.


MainActivity.java:

import java.io.File;
02
03import android.app.Activity;
04import android.content.ActivityNotFoundException;
05import android.content.Intent;
06import android.net.Uri;
07import android.os.Bundle;
08import android.os.Environment;
09import android.view.View;
10import android.view.View.OnClickListener;
11import android.widget.Button;
12import android.widget.Toast;
13
14public class MainActivity extends Activity {
15
16 @Override
17 public void onCreate(Bundle savedInstanceState) {
18  super.onCreate(savedInstanceState);
19  setContentView(R.layout.activity_main);
20
21  Button button = (Button) findViewById(R.id.bPressMe);
22
23  button.setOnClickListener(new OnClickListener() {
24   @Override
25   public void onClick(View v) {
26
27    File pdfFile = new File(Environment
28      .getExternalStorageDirectory(), "Case Study.pdf");
29
30    try {
31     if (pdfFile.exists()) {
32      Uri path = Uri.fromFile(pdfFile);
33      Intent objIntent = new Intent(Intent.ACTION_VIEW);
34      objIntent.setDataAndType(path, "application/pdf");
35      objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
36      startActivity(objIntent);
37     } else {
38      Toast.makeText(MainActivity.this, "File NotFound",
39        Toast.LENGTH_SHORT).show();
40     }
41    } catch (ActivityNotFoundException e) {
42     Toast.makeText(MainActivity.this,
43       "No Viewer Application Found", Toast.LENGTH_SHORT)
44       .show();
45    } catch (Exception e) {
46     e.printStackTrace();
47    }
48   }
49  });
50
51 }
52
53}


4.Include the WRITE_EXTERNAL_STORAGE permission in the manifest file(permission for both reading and writing).
5.Run the application by right clicking the project Run As->Android Application. If everything goes well the output will be similar to the one below



Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home