2013-08-27 81 views
6

我嘗試了下面的代碼。但是,它始終會生成160 * 160尺寸的圖像。通過在Android中傳遞圖像文件路徑來裁剪圖像

try { 
    //call the standard crop action intent (the user device may not support it) 
    Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
    //indicate image type and Uri 
    cropIntent.setDataAndType(Uri.fromFile(pictureFile), "image/*"); 
    //set crop properties 
    cropIntent.putExtra("crop", "true"); 
    //indicate aspect of desired crop 
    cropIntent.putExtra("aspectX", 100); 
    cropIntent.putExtra("aspectY", 100); 
    cropIntent.putExtra("scale", true); 

    //indicate output X and Y 
    cropIntent.putExtra("outputX", 500); 
    cropIntent.putExtra("outputY", 500); 
    //retrieve data on return 
    cropIntent.putExtra("return-data", true); 
    //start the activity - we handle returning in onActivityResult 
    startActivityForResult(cropIntent, CROP_IMAGE); 

} catch(ActivityNotFoundException anfe) { 
    //display an error message 
    String errorMessage = "Whoops - your device doesn't support the crop action!"; 
    Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); 
    toast.show(); 

} 

我想通過傳遞路徑來裁剪圖像。 我不想捕捉/從默認相機應用程序或畫廊中挑選。請幫助我。

回答

11

我已經解決了這個由創建調用意圖並通過意向裁剪後的圖像保存此文件的路徑之前,一個新的文件。這是解決方案。

private Uri mCropImagedUri; 
private final int CROP_IMAGE = 100;//unique request code number. Which is used to identify the request result in onActivityResult() 
/**Crop the image 
* @return returns <tt>true</tt> if crop supports by the device,otherwise false*/ 
private boolean performCropImage(){ 
    try { 
     if(mFinalImageUri!=null){ 
      //call the standard crop action intent (the user device may not support it) 
      Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
      //indicate image type and Uri 
      cropIntent.setDataAndType(mFinalImageUri, "image/*"); 
      //set crop properties 
      cropIntent.putExtra("crop", "true"); 
      //indicate aspect of desired crop 
      cropIntent.putExtra("aspectX", 1); 
      cropIntent.putExtra("aspectY", 1); 
      cropIntent.putExtra("scale", true); 
      //indicate output X and Y 
      cropIntent.putExtra("outputX", 500); 
      cropIntent.putExtra("outputY", 500); 
      //retrieve data on return 
      cropIntent.putExtra("return-data", false); 

      File f = createNewFile("CROP_"); 
      try { 
       f.createNewFile(); 
      } catch (IOException ex) { 
       VLLog.e("io", ex.getMessage()); 
      } 

      mCropImagedUri = Uri.fromFile(f); 
      cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCropImagedUri); 
      //start the activity - we handle returning in onActivityResult 
      startActivityForResult(cropIntent, CROP_IMAGE); 
      return true; 
     } 
    } 
    catch(ActivityNotFoundException anfe){ 
     //display an error message 
     String errorMessage = "Whoops - your device doesn't support the crop action!"; 
     Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); 
     toast.show(); 
     return false; 
    } 
    return false; 
} 

private File createNewFile(String prefix){ 
    if(prefix==null || "".equalsIgnoreCase(prefix)){ 
     prefix="IMG_"; 
    } 
    File newDirectory = new File(Environment.getExternalStorageDirectory()+"/mypics/"); 
    if(!newDirectory.exists()){ 
     if(newDirectory.mkdir()){ 
      VLLog.d(mContext.getClass().getName(), newDirectory.getAbsolutePath()+" directory created"); 
     } 
    } 
    File file = new File(newDirectory,(prefix+System.currentTimeMillis()+".jpg")); 
    if(file.exists()){ 
     //this wont be executed 
     file.delete(); 
     try { 
      file.createNewFile(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    return file; 
} 

所以在這裏我們不應該打擾有關onActivityResult()方法的數據。

這裏是關於裁剪圖像的完整信息。我用這個來解決。 http://www.androidworks.com/crop_large_photos_with_android

+0

如果這是你的首選解決方案,那麼請標記爲這樣,並關閉問題 –

+0

我可以標記爲回答2天后才按照SO規則。 :( – Noundla

+0

經過幾天的努力終於運轉良好,tnx。多麼愚蠢的錯誤!>: – Sdghasemi

-5

通過這種方式,可以縮放圖像:

Bitmap.createScaledBitmap(bitmap,50,50,true); 
+0

我不得不作物基於用戶選擇的圖像,我發佈了答案。 – Noundla

+0

@HarshParikh:實際上也有一個用於此目的的SO徽章(http://stackoverflow.com/help/badges/1/teacher),並且是鼓勵行爲。 –

+0

爲什麼你有投票給我。因此,我已經寫了這條線 –