此图像在Oreo版本中不支持编辑。
(此图像不支持编辑)此Toast在Oreo版本移动设备中从图库中选择图像时显示。我已经问过这个问题,但是没人回答我。请检查我的代码,然后尽快还原。
这是我的代码:-
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != 0) {
if (requestCode == GALLERY_CAPTURE) {
picUri = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContext().getContentResolver().query(picUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
performCrop();
}
else if (requestCode == CROP_PIC) {
Bundle extras = data.getExtras();
Bitmap thePic = extras.getParcelable("data");
rimage.setImageBitmap(thePic);
getCroppedBitmap(thePic);
Uri tempUri = getImageUri(getActivity(), thePic);
finalFile = new File(getRealPathFromURI(tempUri));
Log.e("cropped Image Path", String.valueOf(finalFile));
}
}
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.PNG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
public String getRealPathFromURI(Uri uri) {
Cursor cursor = getContext().getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
public Bitmap getCroppedBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
bitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
private void performCrop() {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP"); cropIntent.setClassName("com.prince","com.prince.RegisterStepThree");
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 96);
cropIntent.putExtra("outputY", 96);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, CROP_PIC);
}
catch (ActivityNotFoundException anfe) {
Toast toast = Toast
.makeText(getContext(), "This device doesn't support the crop action!", Toast.LENGTH_SHORT);
toast.show();
}
catch (Exception e)
{
e.printStackTrace();
}
}
答案 0 :(得分:0)
将Uri添加到文件中
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(<file object>);
我还尝试传递FileProvider.getUriForFile(<file object>)
生成的Uri,并且有效。
答案 1 :(得分:0)
在包括intent.putExtra(MediaStore.EXTRA_OUTPUT, uri)
和ACTION_EDIT
在内的多个操作中,使用MediaStore.ACTION_IMAGE_CAPTURE
似乎是正确的答案。
请注意,在现代API中,应该使用FileProvider创建URI
答案 2 :(得分:0)
这是根据文件路径裁剪图像的代码。我也用 android 11 进行了测试,它工作正常。您还可以在下面的屏幕中看到结果。
在Utils.kt文件中编写的Kotlin函数:
fun performCrop(picUri: File, f: Fragment) {
val packageName: String = f.requireContext().applicationContext.packageName
val authority = "$packageName.fileprovider"
val uri = FileProvider.getUriForFile(f.requireContext(), authority, picUri)
if (Build.VERSION.SDK_INT >= 24) {
try {
val m = StrictMode::class.java.getMethod("disableDeathOnFileUriExposure")
m.invoke(null)
} catch (e: Exception) {
e.printStackTrace()
}
}
try {
val cropIntent = Intent("com.android.camera.action.CROP")
cropIntent.setDataAndType(uri, "image/*")
cropIntent.putExtra("crop", true)
cropIntent.putExtra("aspectX", 1)
cropIntent.putExtra("aspectY", 1)
cropIntent.putExtra("outputX", 512)
cropIntent.putExtra("outputY", 512)
cropIntent.putExtra("return-data", true)
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(picUri))
cropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
cropIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
f.startActivityForResult(
Intent.createChooser(
cropIntent,
"Complete action using"
), Constants.REQUEST_CODE_CROP
)
}
catch (anfe: ActivityNotFoundException) {
val errorMessage = "Whoops - your device doesn't support the crop action!"
val toast = Toast.makeText(f.requireContext(), errorMessage, Toast.LENGTH_SHORT)
toast.show()
} }
从 EditProfile Fragment 调用上述函数:
performCrop(File(editProfileViewModel.filePath), this@EditProfile)