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:
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" > |
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" /> |
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 !!!" /> |
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:
03 | import android.app.Activity; |
04 | import android.content.ActivityNotFoundException; |
05 | import android.content.Intent; |
06 | import android.net.Uri; |
07 | import android.os.Bundle; |
08 | import android.os.Environment; |
09 | import android.view.View; |
10 | import android.view.View.OnClickListener; |
11 | import android.widget.Button; |
12 | import android.widget.Toast; |
14 | public class MainActivity extends Activity { |
17 | public void onCreate(Bundle savedInstanceState) { |
18 | super.onCreate(savedInstanceState); |
19 | setContentView(R.layout.activity_main); |
21 | Button button = (Button) findViewById(R.id.bPressMe); |
23 | button.setOnClickListener(new OnClickListener() { |
25 | public void onClick(View v) { |
27 | File pdfFile = new File(Environment |
28 | .getExternalStorageDirectory(), "Case Study.pdf"); |
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); |
38 | Toast.makeText(MainActivity.this, "File NotFound", |
39 | Toast.LENGTH_SHORT).show(); |
41 | } catch (ActivityNotFoundException e) { |
42 | Toast.makeText(MainActivity.this, |
43 | "No Viewer Application Found", Toast.LENGTH_SHORT) |
45 | } catch (Exception e) { |
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: Android Solutions