从服务器下载的PDF并显示它 [英] Downloading pdf from server and displaying it

查看:76
本文介绍了从服务器下载的PDF并显示它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从服务器上下载PDF格式,并显示它,因为它是。

我尝试以下code片段,但它给我的错误,如下图所示。

我无法理解为什么它给无活动来处理意图例外。

我是新到Android所以任何帮助AP preciated ...

的onCreate()段

 字符串extStorageDirectory = Environment.getExternalStorageDirectory()的toString()。
    文件夹=新的文件(extStorageDirectory,PDF);
    folder.mkdir();
    档案文件=新的文件(文件夹,Read.pdf);
    尝试 {
        file.createNewFile();
    }赶上(IOException异常E1){
        e1.printStackTrace();
    }
    Downloader.DownloadFile(
            http://www.nmu.ac.in/ejournals/aspx/courselist.pdf,文件);

    档案文件1 =新的文件(Environment.getExternalStorageDirectory()
            +/pdf/Read.pdf);
    PackageManager packageManager = getPackageManager();
    意图testIntent =新的意图(Intent.ACTION_VIEW);
    testIntent.setType(应用程序/ PDF格式);
    @燮pressWarnings({rawtypes,未使用})
    列表列表= packageManager.queryIntentActivities(testIntent,
            PackageManager.MATCH_DEFAULT_ONLY);
    意向意图=新的意图();
    intent.setAction(Intent.ACTION_VIEW);
    开放的我们的uri = Uri.fromFile(文件1);
    intent.setDataAndType(URI,应用程序/ PDF格式);
    startActivity(意向);
 

的logcat

 致命异常:主要
java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.pdftest / com.pdftest.PDFFromServerExample}:无活动处理意向{行为= android.intent.action.VIEW DAT =文件:/// MNT / SD卡/ PDF / Read.pdf典型值=应用程序/ PDF}
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)
在android.os.Handler.dispatchMessage(Handler.java:99)
在android.os.Looper.loop(Looper.java:123)
在android.app.ActivityThread.main(ActivityThread.java:4627)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:521)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
在dalvik.system.NativeStart.main(本机方法)
android.content.ActivityNotFoundException:产生的原因无活动处理意向{行为= android.intent.action.VIEW DAT =文件:///mnt/sdcard/pdf/Read.pdf典型值=应用程序/ PDF}
在android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)
在android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
在android.app.Activity.startActivityForResult(Activity.java:2817)
在android.app.Activity.startActivity(Activity.java:2923)
在com.pdftest.PDFFromServerExample.onCreate(PDFFromServerExample.java:44)
在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
 

解决方案

Android不提供任何内置的应用程序,读取PDF内容。所以,你的情况,你正在做的是

 (典型值)=应用程序/ PDF格式
 

但没有应用程序读取或知道这指的是。所以,你必须安装PDF阅读器这可能我任何试图连接起来,(典型值)=应用程序/ PDF ,并将其传递给应用程序阅读PDF文件。

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)

解决方案

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.

这篇关于从服务器下载的PDF并显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆