Downloading pdf from server and displaying it

2020-08-01 07:35发布

问题:

I'm trying to Downloading pdf from server and displaying it as it is.

I tried the following code snippet but it's giving me error as shown below.

I'm not able to understand why it's giving No Activity found to handle Intent exception.

I'm new to android so any help appreciated...

onCreate() snippet

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);

    File file1 = new File(Environment.getExternalStorageDirectory()
            + "/pdf/Read.pdf");
    PackageManager packageManager = getPackageManager();
    Intent testIntent = new Intent(Intent.ACTION_VIEW);
    testIntent.setType("application/pdf");
    @SuppressWarnings({ "rawtypes", "unused" })
    List list = packageManager.queryIntentActivities(testIntent,
            PackageManager.MATCH_DEFAULT_ONLY);
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    Uri uri = Uri.fromFile(file1);
    intent.setDataAndType(uri, "application/pdf");
    startActivity(intent);

Logcat

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pdftest/com.pdftest.PDFFromServerExample}: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/pdf/Read.pdf typ=application/pdf }
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
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 }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
at android.app.Activity.startActivityForResult(Activity.java:2817)
at android.app.Activity.startActivity(Activity.java:2923)
at com.pdftest.PDFFromServerExample.onCreate(PDFFromServerExample.java:44)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

回答1:

Android doesn't provider any inbuilt Application that reads the PDF content. So, in your case you are doing as

typ=application/pdf

But there is no Application that reads or knows what this refers to. So, you have to install PDF reader which may me any that tries to link up typ=application/pdf and passes it to that Application to read the PDF.



回答2:

By default, there is no application that can handle a displaying a pdf, this is often times a third party application. Specifically what you are interested in is the Action of the Intent and how you can catch it and display it if possible.



回答3:

Seem like you do not have pdf reader though you try to read the pdf .

only thing you need to do is install the pdf reader like adobe reader or some else . this code will work fine after that !!!!

And your intention is only to view the pdf and you have internet connection then it would be fine to open your pdf in google docs , like

http://docs.google.com/viewer?url=http://www.nmu.ac.in/ejournals/aspx/courselist.pdf.

Note:(well ..this is alternative of viewing pdf, honestly i am not recommend it )



回答4:

Uri path = Uri.fromFile(dwonload_file_name);
                try {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(path, "application/pdf");
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    finish();
                } catch (ActivityNotFoundException e) {
                    Toast.makeText(
                            context,
                            "PDF Reader application is not installed in your device",
                            Toast.LENGTH_SHORT).show();

                }

Hope it'll be helpful. Full Source Code for "How to download file from Server"