【问题标题】:Viewing a local PDF file in Android Application在 Android 应用程序中查看本地 PDF 文件
【发布时间】:2015-05-08 20:58:39
【问题描述】:

我正在尝试创建一个可以让您查看存储的 PDF 的应用程序,例如一个简单的文件阅读器。

我正在使用导航抽屉项目库,但我似乎无法打开 PDF。

我在 assets/ 中存储了一个测试 PDF,我也尝试在 raw/ 中。如果我尝试使用资产/它在我尝试在设备上打开 PDF 时崩溃,并显示“无法显示 PDF(无法打开 test.pdf)。

我已经尝试了几种方法来尝试让它工作,但没有一个占上风,这是我现在的代码:

public void onSectionAttached(int number){
    switch (number) {
        case 1:
            mTitle = getString(R.string.title_song);

            File pdfFile = new File("/assets/test.pdf");

            Uri path = Uri.fromFile(pdfFile);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(path, "application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            try
            {
                startActivity(intent);
            }
            catch(ActivityNotFoundException e)
            {
                Toast.makeText(MainActivity.this, "It didn't crash!", Toast.LENGTH_LONG).show();
            }



            break;

        case 2:
            mTitle = getString(R.string.title_artist);
            break;
    }


}

【问题讨论】:

标签: android pdf


【解决方案1】:

您正在启动查看文件的意图。意图是启动另一个应用程序。它无权查看您的资产,甚至您的私人文件。将其复制到它确实有权从第一个读取文件的位置,或者您需要实现 ContentProvider 并为他们提供对文件的访问权限。

【讨论】:

  • 更不用说资产不是 Android 文件系统上的文件,所以 new File("/assets/test.pdf") 将不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-06
  • 1970-01-01
  • 2016-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多