从库中裁剪图像并将其设置为我的imageview背景Android

时间:2015-10-20 06:54:02

标签: android imageview

我解决了这个问题,这里是更新的代码,借助于(android:select image from gallery then crop that and show in an imageview发布在stackoverflow中的帖子(感谢Dhaval Patel和atifali)

我的活动课程:

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 个答案:

答案 0 :(得分:0)

基本上你想要改变裁剪矩形的宽高比。为此,只需在代码中注释掉以下几行。

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