私はFile Providerを実装していますが、慎重にドキュメントに従いましたが、アプリ自体でファイルを使用したり、ファイルを共有したりすることはできません。
マニフェストを追加し、ディレクトリ共有用の xml ファイルを作成し、コンテンツ URI を生成し、URI パーミッションを付与し、キャッシュ、外部、内部ディレクトリでファイルを共有しようとしましたが、何も機能していません。
ネットを検索しましたが、コードに欠けているものは何も見つかりませんでした。
以下はコードです:
マニフェスト:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="package.widget.fileprovider"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
ファイルパス.xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="widgetDoc" path="."/>
</paths>
取得:
private Uri getFileUri(Context context, String name){
File newFile = new File(context.getCacheDir() + File.separator + StorageUtil.INTERNAL_DIR, name);
Uri contentUri = FileProvider.getUriForFile(context, "package.widget.fileprovider", newFile);
return contentUri;
}
PDF ファイルにアクセスするためのコード:
Intent target = new Intent(Intent.ACTION_VIEW);
Uri uri = getFileUri(getApplicationContext(), file);
target.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
target.setDataAndType(uri,"application/pdf");
Intent intent = Intent.createChooser(target, "Choose Pdf Viewer...");
startActivity(intent);
画像にアクセスするコード:
imageview.setImageURI(getFileUri(getApplicationContext(), file));
自分のアプリでもこれらのファイルを使用することさえできないので、私が間違っているところを教えてください。
前もって感謝します。