2015-10-20 66 views
1

我解決了這個問題,這裏是用的(android:select image from gallery then crop that and show in an imageview後在計算器(感謝Dhaval帕特爾的幫助和atifali)裁切圖像,並將其設置爲我的ImageView的背景Android的

我的活動類更新的代碼:

public class MainActivity extends Activity { 

private static int RESULT_LOAD_IMAGE = 1; 
String filePath; 
ImageView bitmapView; 
private final int RESULT_CROP = 2; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
FrameLayout wal = (FrameLayout) findViewById(R.id.fm); 
wal.setOnClickListener(new OnClickListener() { 


     @Override 
     public void onClick(View v) { 

      try{ 
      Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
      startActivityForResult(i, RESULT_LOAD_IMAGE); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     }} 
    }); 
} 
     @Override 
     public void onActivityResult(int requestCode, int resultCode, Intent data) { 

      super.onActivityResult(requestCode, resultCode, data); 
      if (requestCode == RESULT_LOAD_IMAGE && resultCode ==RESULT_OK && null != data) { 
       Uri picUri = data.getData(); 
       String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
       Cursor cursor = getContentResolver().query(picUri, 
         filePathColumn, null, null, null); 
       cursor.moveToFirst(); 
       int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
       filePath = cursor.getString(columnIndex); 
       cursor.close(); 
       doCrop(filePath); 

       if (requestCode == RESULT_CROP) { 
        if(resultCode == Activity.RESULT_OK){ 
        Bundle extras = data.getExtras(); 
       Bitmap selectedBitmap = extras.getParcelable("data"); 
         // Set The Bitmap Data To ImageView 
       bitmapview.setImageBitmap(selectedBitmap);        
       bitmapview.setScaleType(ScaleType.FIT_XY); 
        } 
       } 
       } 
      } 

      private void doCrop(String picPath) { 
       try { 


        Intent cropIntent = new Intent("com.android.camera.action.CROP"); 

        File f = new File(picPath); 
        Uri contentUri = Uri.fromFile(f); 

        cropIntent.setDataAndType(contentUri, "image/*"); 

        cropIntent.putExtra("crop", "true"); 
        // indicate aspect of desired crop 
        cropIntent.putExtra("aspectX", 1); 
        cropIntent.putExtra("aspectY", 1); 
        // indicate output X and Y 
        cropIntent.putExtra("outputX", 280); 
        cropIntent.putExtra("outputY", 280); 

        // retrieve data on return 
        cropIntent.putExtra("return-data", true); 
        // start the activity - we handle returning in onActivityResult 
        startActivityForResult(cropIntent, RESULT_CROP); 
       } 
       catch (ActivityNotFoundException anfe) { 
        String errorMessage = "your device doesn't support the crop action!"; 
        Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); 
        toast.show(); 
       } 
      } 

}

+1

請指定您遇到的問題。 – atifali

+0

嗨Atifail,其實圖像越來越裁剪,但imageview的背景反映與原始centercrop圖像instead.I的意思是我從圖片庫中選擇圖像,然後下一個裁剪它,當我點擊保存此裁剪圖像不反映原始圖像從畫廊只坐在imageview。 – Adithya

回答

0

基本上你想改變寬高比作物rectangle.For這一點,只是註釋掉按照你的代碼行。

cropIntent.putExtra("aspectX", 1); 
cropIntent.putExtra("aspectY", 1); 
+0

嗨Atifail,其實圖像越來越裁剪,但imageview的背景反映與原始centercrop圖像而不是。我的意思是我從圖片庫中選擇圖像,然後下一個裁剪它,當我點擊保存此裁剪圖像不反映原始圖像從畫廊只坐在imageview。 – Adithya

+0

您是否將剪裁的圖像設置爲imageView或imageview顯示原始圖像和裁剪圖像。 – atifali

+0

我解決了這個問題,實際上我忘記了通過intent獲取圖片路徑字符串。我爲其他人添加了代碼片段。 – Adithya

相關問題