【问题标题】:Getting exception for image cropping using intent in android lollipop version. How to crop an image using crop intent in above 5.0 versions?在 android lollipop 版本中使用意图进行图像裁剪时出现异常。如何在 5.0 以上版本中使用裁剪意图裁剪图像?
【发布时间】:2015-10-15 09:19:13
【问题描述】:

我有以下用于裁剪图像的代码。它在 android 版本 4 或 OS Kitkat 中运行良好,但在 Android 版本 5 或 OS Lollipop 上无法运行。

我在谷歌上搜索过,但我没有找到完美的答案。

//使用意图裁剪图像

private void doCrop() {
    @SuppressWarnings("unused")
    final ArrayList<CropOptionAdapter.CropOption> cropOptions = new ArrayList<CropOptionAdapter.CropOption>();
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setType("image/*");
    List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent, 0);
    int size = list.size();
    //if no crop applications are available return the process
    if (size == 0) {
        return;
    } else {
        intent.setData(mImageCaptureUri);
        intent.putExtra("outputX", 200);
        intent.putExtra("outputY", 200);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", true);
        if (size > 0) {
            Intent i = new Intent(intent);
            ResolveInfo res = list.get(0);
            i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
            context.startActivityForResult(i, CROP_PHOTO_SELECTED);
        }
    }
}

在上述方法中,我调用裁剪意图,并在 onActivityResult() 中处理结果。低于 5.0 的版本我得到裁剪图像结果数据但在高于 5.0.1 的版本中我得到的数据为空。 下面是我的 onActivityResult()。

//OnActivityResult

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
      switch (requestCode) {
            case ACTION_PHOTO:
            case PICK_FROM_CAMERA:
            case PICK_FROM_GALLERY:
            case CROP_PHOTO_SELECTED:
                    myPhotoCaptureOnResult(requestCode, resultCode, data);
 }

//根据requestcode裁剪图片

public void myPhotoCaptureOnResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == Activity.RESULT_OK) {
        switch (requestCode) {
            case ACTION_PHOTO: {
                if (resultCode == Activity.RESULT_OK) {
                    //setting image from singleton class which is store at time of capture in PhoneIntentActivity
                    if (Singleton.getSingletonObj().getBitmap() != null) {
                        mCapture_photo.setImageBitmap(Singleton.getSingletonObj().getBitmap());
                        mCapture_photo.setPadding(5, 5, 5, 5);
                    }
                }
            }
            break;
            case PICK_FROM_CAMERA:
                if (resultCode == Activity.RESULT_OK) {
                    // getting image uri from sharedpreferences
                    mImageCaptureUri = Uri.parse(context.getSharedPreferences(StringUtils.APP_PREFFS, Context.MODE_PRIVATE).getString(StringUtils.IMAGE_URI_KEY, null));

                    if (mImageCaptureUri != null) {
                        doCrop();
                    } else {
                        // showing toast when unable to capture the image
                        Debug.toastValid(context, "Unable to Capture Image Please Try again ...");
                    }
                }
                break;

            case PICK_FROM_GALLERY:
                if (resultCode == Activity.RESULT_OK) {
                    if (data.getData() != null) {
                        mImageCaptureUri = data.getData();
                        doCrop();
                    } else {
                        // showing toast when unable to capture the image
                        Debug.toastValid(context, "Unable to Capture Image Please Try again ...");
                    }
                }
                break;
            case CROP_PHOTO_SELECTED:
                if (resultCode == Activity.RESULT_OK) {
                    Bundle extras = data.getExtras();
                    if (extras != null) {
                        //getting the photo data from the crop intent
                        Bitmap photo = extras.getParcelable("data");
                        if (photo != null) {
                            mResizedBitMap = getResizedBitmap(photo, newHeight, newWidth);
                            Singleton.getSingletonObj().setBitmap(mResizedBitMap);
                            setBitMap(mResizedBitMap);
                            mCapture_photo.setImageBitmap(Singleton.getSingletonObj().getBitmap());
                            mCapture_photo.setPadding(5, 5, 5, 5);
                        }
                    }
                }
                break;
        }
    }

}

下面是我的 Logcat....

 10-15 14:41:06.250    8332-8332/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.google.android.apps.photos, PID: 8332
java.lang.RuntimeException: Unable to resume activity {com.google.android.apps.photos/com.google.android.apps.photos.photoeditor.intents.EditActivity}: java.lang.UnsupportedOperationException: No 'output' extra specified and can not save to specified inputUri: content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F2236/ACTUAL/1186776521
       at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3017)              at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3048)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1365)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5289)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

   Caused by: java.lang.UnsupportedOperationException: No 'output' extra specified and can not save to specified inputUri: content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F2236/ACTUAL/1186776521
        at com.google.android.apps.photos.photoeditor.intents.EditActivity.a(PG:13312)
        at gpy.a(PG:88)
        at mgf.a(PG:5197)
        at mgg.a(PG:169)
        at mgf.W_(PG:4114)
        at ofw.a(PG:213)
        at oft.a(PG:98)
        at oft.l(PG:209)
        at ogs.onResume(PG:68)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1257)
        at android.app.Activity.performResume(Activity.java:6076)
                 at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3006)

            

【问题讨论】:

标签: android image


【解决方案1】:

默认情况下,保存到内部存储的文件是您的私有 应用程序和其他应用程序无法访问它们(也不能 用户)。

引用自 Android 开发者 Website 上的 Storage Options

ACTION_CROP 启动一个支持裁剪的“其他应用程序”,并将文件的 URI 传递给它以进行裁剪。如果该文件在内部存储中,则裁剪应用程序无法直接访问它。

使用[FileProvider][1] 向其他应用提供内部文件需要进行一些配置。引用自 Android 开发者 Website 上的 Setting Up File Sharing

有关在 Android 5.1.1 上为我工作的更多信息和理想解决方案,请参阅solution

【讨论】:

  • 您只需将cropIntent.setDataAndType(providedUri, "image/*");中所选图像的“providedUri”替换为“imageUri”即可
  • 检查您的相机返回的图像是否不是null
  • 捕获的图像不为空,Intent 裁剪结果返回空。
  • 您是否按照链接中提供的解决方案实施?带有path.xml 和“AndroidManifest”代码。
【解决方案2】:

在 Lollipop 上,裁剪后的图像作为对结果数据操作值中 uri 的字符串引用返回,而不是附加部分。例如:

final String action = data.getAction();
Uri imageUri = Uri.parse(action)

【讨论】:

    猜你喜欢
    • 2015-02-02
    • 1970-01-01
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    • 1970-01-01
    • 2012-01-16
    • 1970-01-01
    相关资源
    最近更新 更多