通过android意图打开在线pdf文件? [英] Open online pdf file through android intent?
问题描述
目前我有一个 pdf url
,我想简单地使用意图打开它,但是,如果我将 url
放在 <代码>意图代码>
Currently I have a pdf url
, and I would like to simply using the intent to open it, however, it does not work if I put the url
in intent
我的代码是这样的,总是抛出ActivityNotFoundException
错误
My code is like this, it always throw ActivityNotFoundException
error
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(law.url), "application/pdf");
try {
startActivity(intent);
} catch (ActivityNotFoundException e){
Utility.showErrorDialog(
ctx,ctx.getResources().getString(R.string.sys_in,
ctx.getResources().getString(R.string.err_no_pdf_reader),
ctx.getResources().getString(R.string.close));
}
我也尝试过goolge doc方法,但我的客户拒绝了,所以我没有使用这种方法
Also I tried the goolge doc approach but my client reject this, so I am not using this method
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("http://docs.google.com/viewer?url="
+ publish.get(Integer.parseInt((String) view.getTag())).pdfURL), "text/html");
ctx.startActivity(intent);
感谢帮助
记录猫错误更新
04-23 18:18:50.487: E/AndroidRuntime(17161): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=http://oshc.zizsoft.com/oshc_testing.pdf typ=application/pdf }
04-23 18:18:50.487: E/AndroidRuntime(17161): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1568)
04-23 18:18:50.487: E/AndroidRuntime(17161): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1439)
04-23 18:18:50.487: E/AndroidRuntime(17161): at android.app.Activity.startActivityForResult(Activity.java:3356)
04-23 18:18:50.487: E/AndroidRuntime(17161): at android.app.Activity.startActivityForResult(Activity.java:3317)
04-23 18:18:50.487: E/AndroidRuntime(17161): at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:848)
04-23 18:18:50.487: E/AndroidRuntime(17161): at android.support.v4.app.Fragment.startActivity(Fragment.java:878)
04-23 18:18:50.487: E/AndroidRuntime(17161): at com.example.oshpedia.Fragment.Shelf$4.onItemClick(Shelf.java:142)
04-23 18:18:50.487: E/AndroidRuntime(17161): at it.sephiroth.android.library.widget.AdapterView.performItemClick(AdapterView.java:299)
04-23 18:18:50.487: E/AndroidRuntime(17161): at it.sephiroth.android.library.widget.AbsHListView.performItemClick(AbsHListView.java:972)
04-23 18:18:50.487: E/AndroidRuntime(17161): at it.sephiroth.android.library.widget.AbsHListView$PerformClick.run(AbsHListView.java:2511)
04-23 18:18:50.487: E/AndroidRuntime(17161): at it.sephiroth.android.library.widget.AbsHListView$1.run(AbsHListView.java:3200)
04-23 18:18:50.487: E/AndroidRuntime(17161): at android.os.Handler.handleCallback(Handler.java:615)
04-23 18:18:50.487: E/AndroidRuntime(17161): at android.os.Handler.dispatchMessage(Handler.java:92)
04-23 18:18:50.487: E/AndroidRuntime(17161): at android.os.Looper.loop(Looper.java:137)
04-23 18:18:50.487: E/AndroidRuntime(17161): at android.app.ActivityThread.main(ActivityThread.java:4882)
04-23 18:18:50.487: E/AndroidRuntime(17161): at java.lang.reflect.Method.invokeNative(Native Method)
04-23 18:18:50.487: E/AndroidRuntime(17161): at java.lang.reflect.Method.invoke(Method.java:511)
04-23 18:18:50.487: E/AndroidRuntime(17161): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
04-23 18:18:50.487: E/AndroidRuntime(17161): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
04-23 18:18:50.487: E/AndroidRuntime(17161): at dalvik.system.NativeStart.main(Native Method)
推荐答案
您可以通过以下两种方式之一查看或下载 pdf,即通过在设备内置浏览器中打开它或通过将其嵌入您的应用程序在 webview 中打开它.
You can view or download the pdf by either of the two ways i.e by opening it in device in-built browser or in the webview by embedding it in your app.
要在浏览器中打开 pdf,
To open the pdf in browser,
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pdf_url));
startActivity(browserIntent);
改为在 webview 中打开,
Instead to open in webview,
WebView webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(pdf_url);
这篇关于通过android意图打开在线pdf文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!