Android Projects

Android Development Books

Tuesday

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

 
From: EduMobile.org
Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

0 comments

Thanks for your comment

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

Related Posts Plugin for WordPress, Blogger...
© Google Android Lovers
Designed by BlogThietKe Cooperated with Duy Pham
Released under Creative Commons 3.0 CC BY-NC 3.0