PDF From Server Example

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"?-->
      android:versionCode="1"
      android:versionName="1.0" package="com.pdftest">
    
    
    
    
                          android:label="@string/app_name">
            
                
                
            
        

    

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", 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 the Author

Leave a Reply 10 comments

vennila vaithi - March 6, 2012 Reply

Hi tried this pdf application , i can download the file, but i cant display it, if i try to display it shows force close,logcat shows the following error, pls reply for my question

03-23 16:36:22.833: W/System.err(270): java.io.IOException: Permission denied
03-23 16:36:22.842: W/System.err(270): at java.io.File.createNewFileImpl(Native Method)
03-23 16:36:22.873: W/System.err(270): at java.io.File.createNewFile(File.java:1160)
03-23 16:36:22.873: W/System.err(270): at com.pdftest.PDFFromServerExample.onCreate(PDFFromServerExample.java:27)
03-23 16:36:22.883: W/System.err(270): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-23 16:36:22.883: W/System.err(270): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-23 16:36:22.883: W/System.err(270): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-23 16:36:22.883: W/System.err(270): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-23 16:36:22.892: W/System.err(270): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-23 16:36:22.892: W/System.err(270): at android.os.Handler.dispatchMessage(Handler.java:99)
03-23 16:36:22.892: W/System.err(270): at android.os.Looper.loop(Looper.java:123)
03-23 16:36:22.892: W/System.err(270): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-23 16:36:22.892: W/System.err(270): at java.lang.reflect.Method.invokeNative(Native Method)
03-23 16:36:22.892: W/System.err(270): at java.lang.reflect.Method.invoke(Method.java:521)
03-23 16:36:22.892: W/System.err(270): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-23 16:36:22.903: W/System.err(270): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-23 16:36:22.903: W/System.err(270): at dalvik.system.NativeStart.main(Native Method)
03-23 16:36:22.922: W/System.err(270): java.io.FileNotFoundException: /mnt/sdcard/pdf/Read.pdf (Permission denied)
03-23 16:36:22.922: W/System.err(270): at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
03-23 16:36:22.922: W/System.err(270): at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
03-23 16:36:22.933: W/System.err(270): at java.io.FileOutputStream.(FileOutputStream.java:97)
03-23 16:36:22.933: W/System.err(270): at java.io.FileOutputStream.(FileOutputStream.java:69)
03-23 16:36:22.933: W/System.err(270): at com.pdftest.Downloader.DownloadFile(Downloader.java:14)
03-23 16:36:22.933: W/System.err(270): at com.pdftest.PDFFromServerExample.onCreate(PDFFromServerExample.java:31)
03-23 16:36:22.933: W/System.err(270): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-23 16:36:22.933: W/System.err(270): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-23 16:36:22.943: W/System.err(270): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-23 16:36:22.943: W/System.err(270): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-23 16:36:22.943: W/System.err(270): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-23 16:36:22.943: W/System.err(270): at android.os.Handler.dispatchMessage(Handler.java:99)
03-23 16:36:22.943: W/System.err(270): at android.os.Looper.loop(Looper.java:123)
03-23 16:36:22.943: W/System.err(270): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-23 16:36:22.943: W/System.err(270): at java.lang.reflect.Method.invokeNative(Native Method)
03-23 16:36:22.943: W/System.err(270): at java.lang.reflect.Method.invoke(Method.java:521)
03-23 16:36:22.943: W/System.err(270): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-23 16:36:22.953: W/System.err(270): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-23 16:36:22.953: W/System.err(270): at dalvik.system.NativeStart.main(Native Method)
03-23 16:36:22.982: D/AndroidRuntime(270): Shutting down VM
03-23 16:36:22.982: W/dalvikvm(270): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-23 16:36:22.993: E/AndroidRuntime(270): FATAL EXCEPTION: main
03-23 16:36:22.993: E/AndroidRuntime(270): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pdftest/com.pdftest.PDFFromServerExample}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/pdf/Read.pdf typ=application/pdf }
03-23 16:36:22.993: E/AndroidRuntime(270): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-23 16:36:22.993: E/AndroidRuntime(270): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-23 16:36:22.993: E/AndroidRuntime(270): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-23 16:36:22.993: E/AndroidRuntime(270): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-23 16:36:22.993: E/AndroidRuntime(270): at android.os.Handler.dispatchMessage(Handler.java:99)
03-23 16:36:22.993: E/AndroidRuntime(270): at android.os.Looper.loop(Looper.java:123)
03-23 16:36:22.993: E/AndroidRuntime(270): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-23 16:36:22.993: E/AndroidRuntime(270): at java.lang.reflect.Method.invokeNative(Native Method)
03-23 16:36:22.993: E/AndroidRuntime(270): at java.lang.reflect.Method.invoke(Method.java:521)
03-23 16:36:22.993: E/AndroidRuntime(270): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-23 16:36:22.993: E/AndroidRuntime(270): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-23 16:36:22.993: E/AndroidRuntime(270): at dalvik.system.NativeStart.main(Native Method)
03-23 16:36:22.993: E/AndroidRuntime(270): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/pdf/Read.pdf typ=application/pdf }
03-23 16:36:22.993: E/AndroidRuntime(270): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)
03-23 16:36:22.993: E/AndroidRuntime(270): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
03-23 16:36:22.993: E/AndroidRuntime(270): at android.app.Activity.startActivityForResult(Activity.java:2817)
03-23 16:36:22.993: E/AndroidRuntime(270): at android.app.Activity.startActivity(Activity.java:2923)
03-23 16:36:22.993: E/AndroidRuntime(270): at com.pdftest.PDFFromServerExample.showPdf(PDFFromServerExample.java:46)
03-23 16:36:22.993: E/AndroidRuntime(270): at com.pdftest.PDFFromServerExample.onCreate(PDFFromServerExample.java:33)
03-23 16:36:22.993: E/AndroidRuntime(270): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-23 16:36:22.993: E/AndroidRuntime(270): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-23 16:36:22.993: E/AndroidRuntime(270): … 11 more
03-23 16:38:28.882: D/AndroidRuntime(353): Shutting down VM
03-23 16:38:28.882: W/dalvikvm(353): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-23 16:38:28.922: E/AndroidRuntime(353): FATAL EXCEPTION: main
03-23 16:38:28.922: E/AndroidRuntime(353): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pdftest/com.pdftest.PDFFromServerExample}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/pdf/Read.pdf typ=application/pdf }
03-23 16:38:28.922: E/AndroidRuntime(353): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-23 16:38:28.922: E/AndroidRuntime(353): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-23 16:38:28.922: E/AndroidRuntime(353): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-23 16:38:28.922: E/AndroidRuntime(353): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-23 16:38:28.922: E/AndroidRuntime(353): at android.os.Handler.dispatchMessage(Handler.java:99)
03-23 16:38:28.922: E/AndroidRuntime(353): at android.os.Looper.loop(Looper.java:123)
03-23 16:38:28.922: E/AndroidRuntime(353): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-23 16:38:28.922: E/AndroidRuntime(353): at java.lang.reflect.Method.invokeNative(Native Method)
03-23 16:38:28.922: E/AndroidRuntime(353): at java.lang.reflect.Method.invoke(Method.java:521)
03-23 16:38:28.922: E/AndroidRuntime(353): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-23 16:38:28.922: E/AndroidRuntime(353): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-23 16:38:28.922: E/AndroidRuntime(353): at dalvik.system.NativeStart.main(Native Method)
03-23 16:38:28.922: E/AndroidRuntime(353): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/pdf/Read.pdf typ=application/pdf }
03-23 16:38:28.922: E/AndroidRuntime(353): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)
03-23 16:38:28.922: E/AndroidRuntime(353): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
03-23 16:38:28.922: E/AndroidRuntime(353): at android.app.Activity.startActivityForResult(Activity.java:2817)
03-23 16:38:28.922: E/AndroidRuntime(353): at android.app.Activity.startActivity(Activity.java:2923)
03-23 16:38:28.922: E/AndroidRuntime(353): at com.pdftest.PDFFromServerExample.showPdf(PDFFromServerExample.java:46)
03-23 16:38:28.922: E/AndroidRuntime(353): at com.pdftest.PDFFromServerExample.onCreate(PDFFromServerExample.java:33)
03-23 16:38:28.922: E/AndroidRuntime(353): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-23 16:38:28.922: E/AndroidRuntime(353): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-23 16:38:28.922: E/AndroidRuntime(353): … 11 more
03-23 16:38:35.212: I/Process(353): Sending signal. PID: 353 SIG: 9
03-23 16:45:14.532: D/AndroidRuntime(377): Shutting down VM
03-23 16:45:14.532: W/dalvikvm(377): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-23 16:45:14.553: E/AndroidRuntime(377): FATAL EXCEPTION: main
03-23 16:45:14.553: E/AndroidRuntime(377): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pdftest/com.pdftest.PDFFromServerExample}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/pdf/Read.pdf typ=application/pdf }
03-23 16:45:14.553: E/AndroidRuntime(377): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-23 16:45:14.553: E/AndroidRuntime(377): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-23 16:45:14.553: E/AndroidRuntime(377): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-23 16:45:14.553: E/AndroidRuntime(377): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-23 16:45:14.553: E/AndroidRuntime(377): at android.os.Handler.dispatchMessage(Handler.java:99)
03-23 16:45:14.553: E/AndroidRuntime(377): at android.os.Looper.loop(Looper.java:123)
03-23 16:45:14.553: E/AndroidRuntime(377): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-23 16:45:14.553: E/AndroidRuntime(377): at java.lang.reflect.Method.invokeNative(Native Method)
03-23 16:45:14.553: E/AndroidRuntime(377): at java.lang.reflect.Method.invoke(Method.java:521)
03-23 16:45:14.553: E/AndroidRuntime(377): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-23 16:45:14.553: E/AndroidRuntime(377): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-23 16:45:14.553: E/AndroidRuntime(377): at dalvik.system.NativeStart.main(Native Method)
03-23 16:45:14.553: E/AndroidRuntime(377): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/pdf/Read.pdf typ=application/pdf }
03-23 16:45:14.553: E/AndroidRuntime(377): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)
03-23 16:45:14.553: E/AndroidRuntime(377): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
03-23 16:45:14.553: E/AndroidRuntime(377): at android.app.Activity.startActivityForResult(Activity.java:2817)
03-23 16:45:14.553: E/AndroidRuntime(377): at android.app.Activity.startActivity(Activity.java:2923)
03-23 16:45:14.553: E/AndroidRuntime(377): at com.pdftest.PDFFromServerExample.showPdf(PDFFromServerExample.java:46)
03-23 16:45:14.553: E/AndroidRuntime(377): at com.pdftest.PDFFromServerExample.onCreate(PDFFromServerExample.java:33)
03-23 16:45:14.553: E/AndroidRuntime(377): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-23 16:45:14.553: E/AndroidRuntime(377): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-23 16:45:14.553: E/AndroidRuntime(377): … 11 more

furqan - June 8, 2012 Reply

this code is not working ,m getting error
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pdftest/com.pdftest.PDFFromServerActivity}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/pdf/Read.pdf typ=application/pdf }

furqan - June 8, 2012 Reply

this code is not working ,m getting error

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pdftest/com.pdftest.PDFFromServerActivity}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/pdf/Read.pdf typ=application/pdf }

bhaskar - November 15, 2012 Reply

Thanks alot.code working fine

Phoommy - January 3, 2013 Reply

Please help me!! . I got exception about – libcore.net.http.HttpConnection

lakshmikanth - February 9, 2013 Reply

code is working fine, good post, thanks a lot.

Prakash Gavade - March 9, 2013 Reply

Thnx a lot for this code…

giamp - May 23, 2013 Reply

Hi,
thanks for the code.
I am interested running this example for learning purposes.
I am a newbie, so can you please post the main.xml ?
Because eclipse finds error (line:19 PDFFromServerActivity.java).
Please answer, it will help a lot.
Thanks in advance.

giamp - May 23, 2013 Reply

Sorry for the previews comment, i used the default main.xml, eclipse didnt find any errors, but when i run the app it closes with unexpected error.

(Unfortunately, PDFFromServerHasStopped)

Also i tried changing the url with a working one.
Can you help me?
Thanks.

Akila - July 3, 2013 Reply

Thanks for ur coding. I got null pointer exception while c.connect() line is executed.

Leave a Reply: