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:

 

 

 

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

 

 

 

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:

 

 

 

3.) Compile and build the project.

Output

Leave a Comment:

11 comments
vennila vaithi says March 6, 2012

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

Reply
furqan says June 8, 2012

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 }

Reply
furqan says June 8, 2012

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 }

Reply
bhaskar says November 15, 2012

Thanks alot.code working fine

Reply
Phoommy says January 3, 2013

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

Reply
lakshmikanth says February 9, 2013

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

Reply
Prakash Gavade says March 9, 2013

Thnx a lot for this code…

Reply
giamp says May 23, 2013

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.

Reply
giamp says May 23, 2013

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.

Reply
Akila says July 3, 2013

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

Reply
Add Your Reply