获取例外android系统版本棒棒糖使用意图图像裁剪。如何裁剪使用裁剪意图在5.0以上版本的图像? [英] Getting exception for image cropping using intent in android lollipop version. How to crop an image using crop intent in above 5.0 versions?

查看:415
本文介绍了获取例外android系统版本棒棒糖使用意图图像裁剪。如何裁剪使用裁剪意图在5.0以上版本的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code用于裁剪图像。它的工作完全在android系统版本4或OS奇巧,但其不工作的Andr​​oid版本5或OS棒棒糖。

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

//使用意图裁剪图片

 私人无效doCrop(){
    @燮pressWarnings(未使用)
    最终的ArrayList< CropOptionAdapter.CropOption> cropOptions =新的ArrayList< CropOptionAdapter.CropOption>();
    意向意图=新意图(com.android.camera.action.CROP);
    intent.setType(图像/ *);
    清单< ResolveInfo> 。名单= context.getPackageManager()queryIntentActivities(意向,0);
    INT大小=则为list.size();
    //如果没有作物的应用都可以回归的过程
    如果(大小== 0){
        返回;
    }其他{
        intent.setData(mImageCaptureUri);
        intent.putExtra(outputX,200);
        intent.putExtra(outputY,200);
        intent.putExtra(aspectX,1);
        intent.putExtra(aspectY,1);
        intent.putExtra(规模化,真正的);
        intent.putExtra(回归数据,真正的);
        如果(大小大于0){
            意图I =新意图(意向);
            ResolveInfo解析度= list.get(0);
            i.setComponent(新组件名(res.activityInfo.packageName,res.activityInfo.name));
            context.startActivityForResult(ⅰ,CROP_PHOTO_SELECTED);
        }
    }
}

在上面的方法我打电话种植意向,并在处理的onActivityResult()的结果。低于5.0的版本我得到作物图像结果DATAM但在上述5.0.1版本中我得到的数据为空。
下面是我的onActivityResult()。

//的onActivityResult

  @覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
    super.onActivityResult(要求code,结果code,数据);
      开关(要求code){
            案例ACTION_PHOTO:
            案例PICK_FROM_CAMERA:
            案例PICK_FROM_GALLERY:
            案例CROP_PHOTO_SELECTED:
                    myPhotoCaptureOnResult(要求code,结果code,数据);
 }

//基于请求code裁剪图片

 公共无效myPhotoCaptureOnResult(INT申请code,INT结果code,意图数据){    如果(结果code == Activity.RESULT_OK){
        开关(要求code){
            案例ACTION_PHOTO:{
                如果(结果code == Activity.RESULT_OK){
                    //从单身类设置的图像,存储在捕获的PhoneIntentActivity时间
                    如果(Singleton.getSingletonObj()。getBitmap()!= NULL){
                        mCapture_photo.setImageBitmap(Singleton.getSingletonObj()getBitmap());
                        mCapture_photo.setPadding(5,5,5,5);
                    }
                }
            }
            打破;
            案例PICK_FROM_CAMERA:
                如果(结果code == Activity.RESULT_OK){
                    //从共享preferences越来越像URI
                    mImageCaptureUri = Uri.parse(context.getShared preferences(StringUtils.APP_ preFFS,Context.MODE_PRIVATE).getString(StringUtils.IMAGE_URI_KEY,NULL));                    如果(mImageCaptureUri!= NULL){
                        doCrop();
                    }其他{
                        //显示敬酒时无法捕捉图像
                        Debug.toastValid(背景下,无法捕捉的图像,请再试一次......);
                    }
                }
                打破;            案例PICK_FROM_GALLERY:
                如果(结果code == Activity.RESULT_OK){
                    如果(data.getData()!= NULL){
                        mImageCaptureUri = data.getData();
                        doCrop();
                    }其他{
                        //显示敬酒时无法捕捉图像
                        Debug.toastValid(背景下,无法捕捉的图像,请再试一次......);
                    }
                }
                打破;
            案例CROP_PHOTO_SELECTED:
                如果(结果code == Activity.RESULT_OK){
                    捆绑额外= data.getExtras();
                    如果(临时演员!= NULL){
                        //从作物意图获取照片数据
                        位图照片= extras.getParcelable(数据);
                        如果(照片!= NULL){
                            mResizedBitMap = getResizedBitmap(照片,newHeight,newWidth);
                            Singleton.getSingletonObj()setBitmap(mResizedBitMap)。
                            setBitMap(mResizedBitMap);
                            mCapture_photo.setImageBitmap(Singleton.getSingletonObj()getBitmap());
                            mCapture_photo.setPadding(5,5,5,5);
                        }
                    }
                }
                打破;
        }
    }}

下面是我的logcat ....

  14 10-15:41:06.250 8332-8332 /? E / AndroidRuntime:致命异常:主要
    工艺:com.google.android.apps.photos,PID:8332
了java.lang.RuntimeException:无法恢复活动{} com.google.android.apps.photos/com.google.android.apps.photos.photoeditor.intents.EditActivity:java.lang.UnsupportedOperationException:没有产出的额外规定而不能保存到指定inputUri: content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F2236/ACTUAL/1186776521
       在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3017)在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3048)
        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1365)
        在android.os.Handler.dispatchMessage(Handler.java:102)
        在android.os.Looper.loop(Looper.java:135)
        在android.app.ActivityThread.main(ActivityThread.java:5289)
        在java.lang.reflect.Method.invoke(本机方法)
        在java.lang.reflect.Method.invoke(Method.java:372)
        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:904)
        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)   java.lang.UnsupportedOperationException:由没有造成产出的额外规定,不能保存到指定inputUri: content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F2236/ACTUAL/1186776521
        在com.google.android.apps.photos.photoeditor.intents.EditActivity.a(PG:13312)
        在gpy.a(PG:88)
        在mgf.a(PG:5197)
        在mgg.a(PG:169)
        在mgf.W_(PG:4114)
        在ofw.a(PG:213)
        在oft.a(PG:98)
        在oft.l(PG:209)
        在ogs.onResume(PG:68)
        在android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1257)
        在android.app.Activity.performResume(Activity.java:6076)
                 在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3006)


解决方案

  

在默认情况下,保存到内部存储器的文件仅对您的
  应用程序和其他应用程序无法访问它们(也可以在
  用户)。


存储选项

在Android开发者< A HREF =htt​​ps://developer.android.com/index.html相对=nofollow>网站。

ACTION_CROP 启动支持种植的其它应用,并将其传递的URI文件裁剪。如果该文件是在内部储存空间,它是不裁剪应用程序直接访问。

使用 [FileProvider] [1] 来提供内部文件到其他应用程序需要一些配置。引用自设置文件共享在Android开发者<一个HREF =htt​​ps://developer.android.com/index.html相对=nofollow>网站。

有关详细信息,并为我工作在Android 5.1.1理想的解决方案,请参阅此解决方案

I have the following code for cropping images. It's working perfectly in android version 4 or OS Kitkat, but its not working on Android version 5 or OS Lollipop.

I have searched in google but I didn't find perfect answer.

//Cropping image using intent

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

In above method i am calling cropping intent, and handling result in onActivityResult(). Below 5.0 versions I am getting crop image result datam But in above 5.0.1 versions I am getting data as null. Below is my 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);
 }

//Cropping image based on 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;
        }
    }

}

Below is my 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)

            

解决方案

By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user).

Referenced from Storage Options on Android Developers Website.

ACTION_CROP launches an "other application" that supports cropping, and passes it the URI of the file to crop. If that file is in internal storage, it is not directly accessible to the cropping application.

Using a [FileProvider][1] to provide an internal file to other apps requires some configuration. Referenced From Setting Up File Sharing on Android Developers Website.

For more information and ideal solution that worked for me on Android 5.1.1, Please see this solution.

这篇关于获取例外android系统版本棒棒糖使用意图图像裁剪。如何裁剪使用裁剪意图在5.0以上版本的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆