2015-10-15 135 views
1

我有下面的代码裁剪图像。它在Android版本4或操作系统Kitkat中运行得非常好,但它不适用于Android版本5或OS Lollipop。在Android棒棒糖版本中使用意图获取图像裁剪异常。如何在5.0版本以上使用裁剪图像裁剪图像?

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

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版本我得到裁剪图像结果datam但在上面的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) 

+0

检查这个库出来http://stackoverflow.com/questions/29190328/any-good-libraries-for-cropping-images-in-android-on-eclipse –

+0

我必须裁剪图像意图裁剪不与自定义裁剪。这些链接与自定义裁剪有关。 –

回答

1

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

在Android开发人员Website上引用自Storage Options

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

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

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

+0

你只需要在这个'cropIntent.setDataAndType(providedUri,“image/*”);' –

+0

替换“providedUri”到你选择的图像的“imageUri”;它不适用于相机捕获的图像。 –

+0

检查你的相机是否返回图像不是“空”。 –