如何裁剪从图库中选择的图像

时间:2014-09-12 10:12:22

标签: android image android-intent

我想使用裁剪意图我可以在使用裁剪意图时从图库中选择图像

然后我的应用程序崩溃了 当我删除Crop Intent然后它的工作正常 但我想裁剪图片...... 这是My Logcat错误

09-12 15:37:08.909: E/AndroidRuntime(25459): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.example.upload_image/com.example.upload_image.MainActivity}: java.lang.NullPointerException
09-12 15:37:08.909: E/AndroidRuntime(25459):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3149)
09-12 15:37:08.909: E/AndroidRuntime(25459):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3192)
09-12 15:37:08.909: E/AndroidRuntime(25459): Caused by: java.lang.NullPointerException
09-12 15:37:08.909: E/AndroidRuntime(25459):    at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1094)

这是我的代码 ..请告诉我在哪里做错了

 public void selectImageFromGallery() {
  Intent intent = new Intent();
  intent.setType("image/*");
  intent.setAction(Intent.ACTION_GET_CONTENT);

  intent.putExtra("crop", "true");
  intent.putExtra("aspectX", 0);
  intent.putExtra("aspectY", 0);
  intent.putExtra("outputX", 200);
  intent.putExtra("outputY", 150);


  try {

      intent.putExtra("return-data", true);
      startActivityForResult(Intent.createChooser(intent, "Select Picture"),
                PICK_IMAGE);
      } catch (ActivityNotFoundException e) {
      // Do nothing for now
      }
      }



 /* startActivityForResult(Intent.createChooser(intent, "Select Picture"),
    PICK_IMAGE);
 }*/


 /**
  * Retrives the result returned from selecting image, by invoking the method
  * <code>selectImageFromGallery()</code>
  */
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);

  if (requestCode == PICK_IMAGE && resultCode == RESULT_OK
    && null != data) {
   Uri selectedImage = data.getData();
   String[] filePathColumn = { MediaStore.Images.Media.DATA };

   Cursor cursor = getContentResolver().query(selectedImage,
     filePathColumn, null, null, null);
   cursor.moveToFirst();

   int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
   String picturePath = cursor.getString(columnIndex);
   cursor.close();

   decodeFile(picturePath);

  }
 }

0 个答案:

没有答案