Hai dalam kode pdf saya memiliki tombol lihat pdf. Ketika mengklik pdf saya mengakses file sebagai string url dan kemudian ingin membuka file pdf. Tetapi ketika saya mencoba mengklik tombol saya mengalami masalah di bawah ini. Dari direktori eksternal

Adakah yang bisa membantu saya menyelesaikan masalah ini.

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....
            }
        }

    });

Dalam manifes:

 <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)
0
user13207116 9 April 2020, 12:50

1 menjawab

Jawaban Terbaik

FileProvider perlu grand akses ke folder dari targetSdkVersion >= 24 .Buat file xml di folder @res/xml provider_paths.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>

Dan Tambahkan ini ke manifes

  <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>

Perubahan

 Uri path = Uri.fromFile(file);

Untuk

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

Dan ubah

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);

Semoga ini membantu.....

0
Android_id 9 April 2020, 12:45