3

对于从 Lollipop 及以上版本的 android 手机拍摄的裁剪图像一个应该使用文件提供程序这里是我的代码。

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

<paths>
    <external-path name="myexternalimages" path="dcim/ProfileImage/"/>
</paths>





 final  Uri providedUri = FileProvider.getUriForFile(ProfileUpdateActivity.this,
                    "com.example.test.fileprovider", imageUpLoadFile);
            Intent cropIntent = new Intent("com.android.camera.action.CROP");



            //indicate image type and Uri
            cropIntent.setDataAndType(providedUri, "image/*");
            //set crop properties
            cropIntent.putExtra("crop", "true");
            //indicate aspect of desired crop
            cropIntent.putExtra("aspectX", 1);
            cropIntent.putExtra("aspectY", 1);
            //indicate output X and Y
            cropIntent.putExtra("outputX", 256);
            cropIntent.putExtra("outputY", 256);
            //retrieve data on return
            cropIntent.putExtra("return-data", true);

            // Exception will be thrown if read permission isn't granted
            cropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

            startActivityForResult(cropIntent, PIC_CROP);

where imageUploadFile =/storage/emulated/0/dcim/ProfileImage/IMG_20160330_134823_1697877403.png (Example)

现在,当它成功返回 onActivityResult 时,我收到一条错误消息,因为无法编辑此图像

4

1 回答 1

1

将 EXTRA_OUTPUT 添加到意图为我修复了它。

cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, providedUri);  

但请确保还向 Intent 添加写入 uri 权限。
以便相机应用程序可以将裁剪后的图像写入提供的 Uri。

cropIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
于 2016-09-08T09:06:18.983 回答