Ras*_*ana 9 pdf android android-intent android-activity
我搜索了很多寻找解决方案,但仍然找不到任何解决方案
我想在我的Android应用程序中打开一个PDF文件
这是我的代码:
try
{
File file = new File(Environment.getExternalStorageDirectory()+"/pdf/Read.pdf");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
Log.d("CheckingURI", uri.toString());
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
}
catch (Exception e)
{
Log.d("OpenPDFError", e.getMessage());
}
Run Code Online (Sandbox Code Playgroud)
Tomcat日志意味着没有活动来处理意图
09-19 19:55:02.938: D/CheckingURI(30483): file:///mnt/sdcard/pdf/Read.pdf
09-19 19:55:02.948: D/OpenPDFError(30483): No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/pdf/Read.pdf typ=application/pdf }
Run Code Online (Sandbox Code Playgroud)
有没有其他方法可以做到这一点,或者我可以用另一个默认的PDF查看器打开PDF文件?如果有,怎么样?
更新: 主要问题是我没有在我的模拟器上安装PDF查看器应用程序...
试试这个:
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/pdf/Read.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
CAVEAT:只有预安装的PDF Viewer才能使用上述方法.如果没有,则需要安装PDF Viewer.
如果没有发现启动 PDF 的活动,您只需向用户弹出某种错误消息(在错误对话框中,您可以让用户知道他需要一些 PDF 应用程序并将其发送到 Play 商店),尽管我'建议使用这段代码而不是您正在使用的通用异常捕获器:
try {
/* your code */
...
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
或者您可以使用一些 PDF 库,例如这个。
| 归档时间: |
|
| 查看次数: |
19861 次 |
| 最近记录: |