2014-03-13 62 views
0

我有这样的代码从图片库,但即使有intent.putExtra("crop", "true");获取图像,但不会裁剪

Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    intent.setType("image/*"); 
    intent.putExtra("crop", "true"); 
    intent.putExtra("aspectX", 0); 
    intent.putExtra("aspectY", 0); 
    intent.putExtra("outputX", 360); 
    intent.putExtra("outputY", 360); 
try 
    { 
     intent.putExtra("return-data", true); 
     startActivityForResult(Intent.createChooser(intent, "Complete action using"), req_code); 
    } 
    catch(ActivityNotFoundException e) 
    { 
     // Do nothing for now 
    } 

获取的图像,选择图像后,它不会显示任何作物活动或任何...为什么?

+0

',将scaleType设置为CenterCrop – longkai

+0

这实际上非常有帮助!我要这样做,直到我找到合适的方式来裁剪图像。 – fcasanova

+0

好吧,如果你的图像非常大,它会出现'outOfMemory'问题,你最好按照谷歌的开发指南或使用库来裁剪大图像。 – longkai

回答

1

因为它不应该。仅仅因为你随机附加随机附加物件并不会奇迹般地迫使第三方应用程序去做他们不会做的事情。

这是the documentation for ACTION_GET_CONTENT。请注意,文档中没有您列出的其他附加内容。因此,没有任何第三方应用程序是必然会期待这些额外的。

Android没有内置的图像裁剪功能可供开发人员使用。虽然有很多图像裁剪库可用。

0
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); 
      photoPickerIntent.setType("image/*"); 
      startActivityForResult(photoPickerIntent, SELECT_PHOTO); 
-1
protected void onActivityResult(int requestCode, int resultCode, 
     Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch (requestCode) { 
    case SELECT_PHOTO: //Select photo == 1 
     if (resultCode == RESULT_OK) { 
      try { 
       final Uri imageUri = imageReturnedIntent.getData(); 
       Bitmap selectedImage = BitmapFactory 
         .decodeStream(getContentResolver().openInputStream(
           imageUri)); 
0

试试这个代码: (为了得到来自GALLARY图像)在你的``ImageView`

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); 
      photoPickerIntent.setType("image/*"); 
      startActivityForResult(photoPickerIntent, GALLERY_REQUEST); 
      dialog.dismiss(); 

(从GALLARY调用此之后拾取图像)

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    Bitmap bmp = null; 
    if (resultCode == RESULT_OK) { 
     try { 

      if (requestCode == GALLERY_REQUEST) { 
       // Gallery request result 
       mImageCaptureUri = data.getData(); 

       TEMP_PHOTO_FILE_NAME = new File(

       startCropImage(); 

     } catch (Exception e) { 
      e.printStackTrace(); 
      File f = new File(getRealPathFromURI(mImageCaptureUri)); 
      if (f.getName().startsWith(FILE_NAME)) { 
       if (f.exists()) 
        f.delete(); 
      } 
     } 
    } 
} 

private void startCropImage() { 


    Intent intent = new Intent("com.android.camera.action.CROP"); 
    intent.setType("image/*"); 

    /** 
    * Check if there is image cropper app installed. 
    */ 
    List<ResolveInfo> list = getPackageManager().queryIntentActivities(
      intent, 0); 

    int size = list.size(); 
      /** 
    * If there is no image cropper app, display warning message 
    */ 
    if (size == 0) { 

     Toast.makeText(this, "Can not find image crop app", 
       Toast.LENGTH_SHORT).show(); 

     return; 
    } else { 
     /** 
     * Specify the image path, crop dimension and scale 
     */ 
     intent.setData(mImageCaptureUri); 

     intent.putExtra("outputX", 256); 
     intent.putExtra("outputY", 256); 
     intent.putExtra("aspectX", 1); 
     intent.putExtra("aspectY", 1); 
     intent.putExtra("scale", true); 
     intent.putExtra("return-data", true); 
     /** 
     * There is posibility when more than one image cropper app exist, 
     * so we have to check for it first. If there is only one app, open 
     * then app. 
     */ 

     if (size == 1) { 
      Intent i = new Intent(intent); 
      ResolveInfo res = list.get(0); 

      i.setComponent(new ComponentName(res.activityInfo.packageName, 
        res.activityInfo.name)); 

      startActivityForResult(i, CROP_FROM_CAMERA); 
     } 
} 
+0

Android中没有'CROP'' Intent':http://commonsware.com/blog/2013/01/23/no-android-does-not-have-crop-intent.html – CommonsWare

+0

请检查我的更新回答 – Vijju

+0

有才华的程序员会使用一个库,而不是依赖可能存在或不存在的未公开解决方案。 – CommonsWare