我正在尝试从服务器下载pdf并按原样显示它。
我尝试了以下代码段,但它给了我错误,如下所示。
我无法理解为什么它会给No Activity found to handle Intent
例外。
我是android新手,所以任何帮助都赞赏...
onCreate()摘录
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)
答案 0 :(得分:2)
默认情况下,没有可以处理显示pdf的应用程序,这通常是第三方应用程序。具体而言,您感兴趣的是意图的动作以及如何捕获它并在可能的情况下显示它。
答案 1 :(得分:1)
Android不会提供任何读取PDF内容的内置应用程序。所以,在你的情况下,你正在做
typ=application/pdf
但是没有应用程序可以读取或知道这指的是什么。因此,您必须安装PDF阅读器,我可以尝试链接typ=application/pdf
并将其传递给该应用程序以阅读PDF。
答案 2 :(得分:1)
虽然您尝试阅读pdf,但似乎没有 pdf阅读器。
你唯一需要做的就是安装pdf阅读器,如adobe reader或其他一些东西。这个代码在那之后会正常工作!!!!
你的目的只是查看pdf并且你有互联网连接然后在谷歌文档中打开你的pdf就好了,比如
http://docs.google.com/viewer?url=http://www.nmu.ac.in/ejournals/aspx/courselist.pdf
注意:(嗯..这是查看pdf的替代方法,老实说我不推荐它)
答案 3 :(得分:0)
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();
}
希望它会有所帮助。 Full Source Code for "How to download file from Server"