从服务器下载pdf并显示

发布于 2025-01-01 03:50:58 字数 3038 浏览 1 评论 0原文

我正在尝试从服务器下载 pdf 并按原样显示。

我尝试了以下代码片段,但它给了我错误,如下所示。

我无法理解为什么它会给出 No Activity found to handle Intent 异常。

我是 android 新手,所以任何帮助都会感激...

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

老街孤人 2025-01-08 03:50:58

默认情况下,没有可以处理显示 pdf 的应用程序,这通常是第三方应用程序。具体来说,您感兴趣的是意图的操作以及如何捕获它并在可能的情况下显示它。

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.

小嗲 2025-01-08 03:50:58

Android 不提供任何读取 PDF 内容的内置应用程序。因此,在您的情况下,您正在做

typ=application/pdf

但是没有应用程序读取或知道这指的是什么。因此,您必须安装 PDF 阅读器,它可以尝试链接 typ=application/pdf 并将其传递给该应用程序以读取 PDF。

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.

情泪▽动烟 2025-01-08 03:50:58

虽然您尝试阅读 pdf,但似乎您没有 pdf 阅读器。

您唯一需要做的就是安装 pdf 阅读器,例如 adobe reader 或其他一些。此后代码将正常工作!!!!

您的目的只是查看 pdf 并且您有互联网连接,那么在 google docs 中打开您的 pdf 就可以了,例如

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

注意:(嗯..这是查看pdf的替代方法,老实说我不推荐它)

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 )

南风起 2025-01-08 03:50:58
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();

                }

希望它会有所帮助。 “如何从服务器下载文件”的完整源代码”

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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文