4

シナリオは次のとおりです。

ネットワークからPDFをダウンロードして保存しています/0/Download/NSIT - Notices/"fileName".pdf

次のようにインテントを送信します。

private void openPDF(String fileName) {
    Log.d(TAG, "openPDF: called");
    Intent intent = new Intent(Intent.ACTION_VIEW);File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download", "NSIT - Notices");
    File myPDF = new File(Environment.getExternalStorageDirectory() + "/Download/NSIT - Notices", fileName + ".pdf");
    Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", myPDF);
    Log.d(TAG, "openPDF: intent with uri: " + uri);
    intent.setDataAndType(uri, "application/pdf");
    context.startActivity(Intent.createChooser(intent, "Open with..."));
}

現在、どの PDF リーダー (Google Drive PDF Reader、System PDF Viewer) も言っています。

The File Does Not Exist

画像: ここに画像の説明を入力

Googleのものは何もせずに座っています(そのため、その画像は含まれていません)

Mainfest.xml

<provider
        android:name="android.support.v4.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>

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

ここで AsyncTask にファイルを作成していますが、その AsyncTask の postExecute から開いています

File myDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download", "NSIT - Notices");
            File myPDF = new File(myDir, params[1] + ".pdf");
            if (!myDir.exists())
                myDir.mkdirs();
            try {
                fileOutputStream = new FileOutputStream(myPDF);
                fileOutputStream.write(response.bodyAsBytes());
                fileOutputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Log.d(TAG, "doInBackground: Download complete");

ログキャット

ここに画像の説明を入力

以前は投げていましたがFileUriExposedException、このことで修正しましたFileProvider。問題は、アプリによってダウンロードされた pdf ファイルが正常に作成され、任意のファイル マネージャー (現在は ES ファイル エクスプローラー) からは正常に開くことができますが、アプリからは開くことができないことです:( FileProvider

PS: それは私のデバイスの問題でもありません。ROM と API >= 23 が異なる他の 2 台の電話でテスト済み

4

2 に答える 2