2013-08-27 9 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なんて愚かなバグ!>:O – Sdghasemi

-5

は、あなたが画像を拡大縮小することができます

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

私はトリミングする必要がありますユーザーの選択に基づいた画像と答えを投稿しました。 – Noundla

+0

@HarshParikh:実際にはこの目的のためのSOバッジもあります(http://stackoverflow.com/help/badges/1/teacher)。 –

+0

なぜあなたは私に投票して下さったのですか。私はこの行を書いています –

関連する問題