Image representing Android as depicted in Crun...

Description:
This example shows you how to download a pdf file from server and display its contents.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it PDFFromServerExample.
2.) Write following code into your manifest file:

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:versionCode=”1″
android:versionName=”1.0″ package=”com.pdftest”>
<uses-sdk android:minSdkVersion=”7″ />
<uses-permission android:name=”android.permission.INTERNET”></uses-permission>
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”></uses-permission>
<application android:icon=”@drawable/icon” android:label=”@string/app_name” android:debuggable=”true”>
<activity android:name=”.PDFFromServerActivity”
android:label=”@string/app_name”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>    </application>
</manifest>

3.) Create Downloader.java file into your package and write following code:

package com.pdftest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class Downloader {        public static void DownloadFile(String fileURL, File directory) {
try {

FileOutputStream f = new FileOutputStream(directory);
URL u = new URL(fileURL);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod(“GET”);
c.setDoOutput(true);
c.connect();

InputStream in = c.getInputStream();

byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = in.read(buffer)) > 0) {
f.write(buffer, 0, len1);
}
f.close();
} catch (Exception e) {
e.printStackTrace();
}

}
}

4.) Run for output.

Steps:

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

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

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

package com.pdftest;import java.io.File;
import java.io.IOException;
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;
import android.os.Environment;

public class PDFFromServerActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String extStorageDirectory = Environment.getExternalStorageDirectory()
.toString();
File folder = new File(extStorageDirectory, “pdf”);
folder.mkdir();
File file = new File(folder, “Read.pdf”);
try {
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
Downloader.DownloadFile(“http://www.nmu.ac.in/ejournals/aspx/courselist.pdf&#8221;, file);

showPdf();
}
public void showPdf()
{
File file = new File(Environment.getExternalStorageDirectory()+”/pdf/Read.pdf”);
PackageManager packageManager = getPackageManager();
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType(“application/pdf”);
List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
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.

Output

About these ads
Comments
  1. Muhammad Zia Shahid says:

    good zeeshan .keep it up …..

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s