2015-10-15 90 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

檢查你的相機是否返回圖像不是「空」。 –