You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android 11调用com.android.camera.action.CROP裁剪图片出现空白问题求助

Android 11图片裁剪界面空白问题

我参考社区示例处理Android 11的图片裁剪功能,目前能正常选择并展示图片,但将选中图片的Uri传入裁剪方法后,裁剪界面显示空白图。不确定是代码逻辑问题还是权限配置问题,已尝试以下两段裁剪代码,均未解决问题,现附上AndroidManifest.xml配置信息:

尝试的裁剪代码1

public static void cropUri(ActivityResultLauncher<Intent> activityResultLauncher, final Uri selectedUri, int width, int height) {
    Intent intent = new Intent("com.android.camera.action.CROP");
    // 打开相册所有图片
    intent.setDataAndType(selectedUri, "image/*");
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    intent.putExtra("crop", true);
    // 设置宽高比例
    intent.putExtra("aspectX", width);
    intent.putExtra("aspectY", height);
    intent.putExtra("return-data", true);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, selectedUri);
    activityResultLauncher.launch(intent);
}

尝试的裁剪代码2

private void cropImage(ActivityResultLauncher<Intent> activityResultLauncher, final Uri selectedUri, int width, int height) {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        grantUriPermission("com.android.camera", selectedUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(selectedUri, "image/*");
        List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, 0);
        int size = 0;
        if(list != null) {
            grantUriPermission(list.get(0).activityInfo.packageName, selectedUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
            size = list.size();
        }
        if (size == 0) {
            Toast.makeText(this, "错误:未获取到图片!", Toast.LENGTH_SHORT).show();
        } else {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            intent.putExtra("crop", "true");
            intent.putExtra("aspectX", width);
            intent.putExtra("aspectY", height);
            intent.putExtra("return-data", false);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, selectedUri);
            intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
            Intent cropIntent = new Intent(intent);
            ResolveInfo res = list.get(0);
            cropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            cropIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            grantUriPermission(res.activityInfo.packageName, selectedUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
            cropIntent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
            activityResultLauncher.launch(cropIntent);
        }
    } else {
        Intent intentCrop = new Intent("com.android.camera.action.CROP");
        intentCrop.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        intentCrop.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intentCrop.setDataAndType(selectedUri, "image/*");
        intentCrop.putExtra("crop", "true");
        intentCrop.putExtra("scale", true);
        intentCrop.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intentCrop.putExtra("noFaceDetection", true);
        intentCrop.putExtra("return-data", false);
        intentCrop.putExtra(MediaStore.EXTRA_OUTPUT, selectedUri);
        cropResultLauncher.launch(intentCrop);
    }
}

AndroidManifest.xml配置

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- 设备重启后自动重复启动闹钟所需权限 -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<queries>
    <intent>
        <action android:name="android.media.action.IMAGE_CAPTURE" />
    </intent>
    <intent>
        <action android:name="com.android.camera.action.CROP" />
        <data
            android:scheme="content"
            android:mimeType="image/*" />
    </intent>
    <intent>
        <action android:name="android.intent.action.PICK" />
        <data android:mimeType="vnd.android.cursor.dir/image" />
    </intent>
</queries>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:windowSoftInputMode="stateAlwaysVisible|adjustPan">
    <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="${applicationId}.fileProvider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths" />
    </provider>
    <activity
        android:name=".ConnectActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
        </intent-filter>
    </activity>
    <activity
        android:name=".PrefsActivity"
        android:label="@string/prefsTitle" />
    <activity
        android:name=".RangtActivity"
        android:label="@string/rangtTitle"
        android:parentActivityName=".ConnectActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".ConnectActivity" />
    </activity>
    <activity
        android:name=".StockActivity"
        android:label="@string/stockTitle"
        android:parentActivityName=".RangtActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".RangtActivity" />
    </activity>
    <activity
        android:name=".ProductActivity"
        android:label="@string/productTitle"
        android:parentActivityName=".StockActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".StockActivity" />
    </activity>
    <activity android:name=".barcode.BarcodeCaptureActivity" />
    <activity android:name=".NotificationActivity" />
</application>

</manifest>

内容的提问来源于stack exchange,提问作者Chris972

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 03:45:37