I'm trying to follow the example of @bigant02 on Can not resolve com.android.camera.action.CROP in android 11 for first time app is installed (not enough reputation to post there) but, if I can pick my photo and show it if needed, if I send this Uri to a cropImage(), I get empty image to crop capture I don't understand if it is a problem with my code or a permission problem
my code :
public static void cropUri(ActivityResultLauncher<Intent> activityResultLauncher, final Uri selectedUri, int width, int height) {
Intent intent = new Intent("com.android.camera.action.CROP");
// this will open all images in the Galery
intent.setDataAndType(selectedUri, "image/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.putExtra("crop", true);
// this defines the aspect ration
intent.putExtra("aspectX", width);
intent.putExtra("aspectY", height);
intent.putExtra("return-data", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, selectedUri);
activityResultLauncher.launch(intent);
}
Tried too with :
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, "Error, wasn't taken image!", 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);
}
}
not better.
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" />
<!-- permission required to automatically restart a repeating alarm if the user reboots the device -->
<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>