2014-03-13 105 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