【问题标题】:Pdf file viewing facing an issue in androidpdf文件查看在android中遇到问题
【发布时间】:2020-04-09 09:50:25
【问题描述】:

嗨,在 pdf 代码中,我有一个按钮查看 pdf。当单击 pdf 时,我以字符串 url 的形式访问文件,然后想要打开 pdf 文件。但是当我尝试单击下面的按钮时问题。来自外部目录

谁能帮我解决这个问题。

pdf:

holder.pdf.setOnClickListener(new View.OnClickListener() {

        @Override public void onClick(View v) {
            File file = new File(Environment.getExternalStorageDirectory(),
                    opportunity.getPdf_link());
            Uri path = Uri.fromFile(file);
            Uri pdf = FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".provider", file);

            Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
            pdfOpenintent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            pdfOpenintent.setDataAndType(pdf, "application/pdf");

            try {
                mContext.startActivity(pdfOpenintent);
            } catch (ActivityNotFoundException e) {
                // handle no application here....
            }
        }

    });

在清单中:

 <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>

 android.os.FileUriExposedException: file:///storage/emulated/0/https%3A/www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf exposed beyond app through Intent.getData()
        at android.os.StrictMode.onFileUriExposed(StrictMode.java:1978)
        at android.net.Uri.checkFileUriExposed(Uri.java:2371)
        at android.content.Intent.prepareToLeaveProcess(Intent.java:10247)
        at android.content.Intent.prepareToLeaveProcess(Intent.java:10201)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1667)
        at android.app.Activity.startActivityForResult(Activity.java:4586)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767)
        at android.app.Activity.startActivityForResult(Activity.java:4544)

【问题讨论】:

  • 您的堆栈跟踪与您的代码不匹配。您的堆栈跟踪显示对startActivityForResult() 的调用,而您的代码显示对startActivity() 的调用。
  • 你能解释一下我不明白...
  • @CommonsWare 无法访问此文件。请检查位置或网络,然后重试我现在面临的这个问题

标签: android file pdf


【解决方案1】:

FileProvider 需要从 targetSdkVersion >= 24 访问文件夹。在 @res/xml 文件夹 provider_paths.xml 中创建一个 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

并将其添加到清单

  <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

改变

 Uri path = Uri.fromFile(file);

Uri uri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider",file);

改变

Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
File file = new File(Environment.getExternalStorageDirectory(),
                        opportunity.getPdf_link());
               // Uri path = Uri.fromFile(file);
                Uri pdf = FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".provider", file);


                pdfOpenintent.setDataAndType(pdf, "application/pdf");
                pdfOpenintent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

mContext.startActivity(intent);

希望这会有所帮助.....

【讨论】:

  • @res/layout 文件夹
  • 我收到一个错误,无法访问此文件。请检查位置或网络,然后重试
  • pdfOpenintent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);这也是同样的问题
  • 进程:com.genworks.oppm,PID:18800 java.lang.StringIndexOutOfBoundsException:长度=19; index=20 at java.lang.String.substring(String.java:1999) at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:748)
  • java.lang.StringIndexOutOfBoundsException: length=19; index=20 at java.lang.String.substring(String.java:1999) at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:748) at androidx.core.content.FileProvider.getUriForFile(FileProvider. java:418) 是的,我遇到了一个问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-04
  • 1970-01-01
  • 1970-01-01
  • 2021-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多