从存储选项 | 安卓开发者:
默认情况下,保存到内部存储的文件对您的应用程序是私有的,其他应用程序无法访问它们(用户也不能)。
ACTION_CROP
启动一个支持裁剪的“其他应用程序”,并将文件的 URI 传递给它以进行裁剪。如果该文件在内部存储中,则裁剪应用程序无法直接访问它。
使用 aFileProvider
向其他应用程序提供内部文件需要一些配置。从设置文件共享 | 安卓开发者:
指定文件提供者
为您的应用定义 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);