Hi Friends, if you like my blog please give your valuable comments it will help to improve my blog content and enthusiasm to write a lot in android World.

Wednesday, February 29, 2012

PDF Reader in Android


This example shows you how to open a pdf file from your activity.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it PDFReader.
2.) You will see some default code into your main.xml and android manifest file.
3.) Download and install any adobe reader application from android market.
4.) Write following into main.xml file:


android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_height="wrap_content" android:text="PDFReader" />

5.) Copy any pdf file onto your sdcard.
6.) Make sure to change the name and path of your pdf file in PDFReaderActivity class. In this example I have used “/sdcard/sample.pdf” . You can change the name of your pdf instead of sample.pdf.
7.) Run for output.

Steps:

1.) Create a project named PDFReader and set the information as stated in the image.

Build Target: Android 2.2
Application Name: PDFReader
Package Name: com.pdftest
Activity Name: PDFReaderActivity
Min SDK Version: 8



2.) Open PDFReaderActivity.java file and write following code there:

package com.pdftest;

import java.io.File;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;

public class PDFReaderActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File file = new File(“/sdcard/sample.pdf”);
PackageManager packageManager = getPackageManager();
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType(“application/pdf”);
List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (list.size() > 0 && file.isFile()) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, “application/pdf”);
startActivity(intent);
}
}
}

3.) Compile and build the project.
4.) Make sure you have installed pdf viewer application on your device or simulator.

Output





No comments:

Post a Comment