【问题标题】:Editing is not supported for this Image in Oreo Version problem此图像在奥利奥版本问题中不支持编辑
【发布时间】:2019-02-06 05:20:42
【问题描述】:

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();
    }
}

【问题讨论】:

  • 如果您尝试编辑受版权保护的照片或不属于您的照片,那么该消息将是正确的。
  • 你找到解决办法了吗?

标签: android crop


【解决方案1】:

将Uri添加到intent文件中

intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(<file object>);

我还尝试传递 FileProvider.getUriForFile(<file object>) 生成的 Uri 并且成功了。

【讨论】:

  • 附带说明,在应用程序之间使用 Uri 时,应始终使用 FileProvider.getUriForFile() 方法,因为另一个方法可能会产生意想不到的结果。
【解决方案2】:

在包括ACTION_EDITMediaStore.ACTION_IMAGE_CAPTURE 在内的多个操作中,使用intent.putExtra(MediaStore.EXTRA_OUTPUT, uri) 似乎是正确的答案。

请注意,在现代 API 中,应该使用 FileProvider 创建 URI

【讨论】:

    【解决方案3】:

    这是根据文件路径裁剪图像的代码。我也已经在 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)
    

    第一步:从图库中选择图片

    Step2:根据需要裁剪图像

    Step3:在Imageview中设置图片

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 2018-10-08
      • 1970-01-01
      相关资源
      最近更新 更多