从服务器下载pdf并显示
我正在尝试从服务器下载 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
默认情况下,没有可以处理显示 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.
Android 不提供任何读取 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
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,但似乎您没有 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 )
希望它会有所帮助。 “如何从服务器下载文件”的完整源代码”
Hope it'll be helpful. Full Source Code for "How to download file from Server"