ストレージ オプションから| Android 開発者:
デフォルトでは、内部ストレージに保存されたファイルはアプリケーション専用であり、他のアプリケーションはそれらにアクセスできません (ユーザーもアクセスできません)。
ACTION_CROP
トリミングをサポートする「他のアプリケーション」を起動し、トリミングするファイルの URI を渡します。そのファイルが内部ストレージにある場合、トリミング アプリケーションから直接アクセスすることはできません。
を使用しFileProvider
て他のアプリに内部ファイルを提供するには、いくつかの構成が必要です。ファイル共有の設定から| Android 開発者:
FileProvider を指定する
アプリの FileProvider を定義するには、マニフェストにエントリが必要です。このエントリは、コンテンツ URI の生成に使用する機関と、アプリが共有できるディレクトリを指定する XML ファイルの名前を指定します。
<中略>
共有可能なディレクトリを指定する
FileProvider をアプリ マニフェストに追加したら、共有するファイルを含むディレクトリを指定する必要があります。ディレクトリを指定するには、プロジェクトの res/xml/ サブディレクトリに filepaths.xml ファイルを作成することから始めます。
次のFileProvider
サンプルはコードを統合し、内部ストレージ内の画像のクロップ アクティビティを正常に開きます (Nexus 5、ストック Android 5.1.1 でテスト済み)。
AndroidManifest.xml
<application
...>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.test.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
...
</application>
res/xml/filepaths.xml
<paths>
<files-path path="images/" name="images" />
</paths>
MainActivity.java
// Manually stored image for testing
final File imagePath = new File(getFilesDir(), "images");
final File imageFile = new File(imagePath, "sample.jpg");
// Provider authority string must match the one declared in AndroidManifest.xml
final Uri providedUri = FileProvider.getUriForFile(
MainActivity.this, "com.example.test.fileprovider", imageFile);
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(providedUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 9);
cropIntent.putExtra("aspectY", 16);
cropIntent.putExtra("return-data", true);
cropIntent.putExtra("scale", true);
// Exception will be thrown if read permission isn't granted
cropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(cropIntent, PIC_CROP);