简体   繁体   English

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

[英]Crop Image from gallery and set it as my imageview background Android

I resolved the issue , here is the updated code with the help of ( android:select image from gallery then crop that and show in an imageview post in stackoverflow(Thanks to Dhaval Patel and atifali) 我解决了这个问题,这是( android:从画廊中选择图像然后裁剪并在stackoverflow 中的imageview帖子中显示的帮助下的更新代码(感谢Dhaval Patel和atifali)

my activity class: 我的活动课:

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();
                }
            }   

} }

Basically you want to change aspect ratio in crop rectangle.For this, just comment out following lines in your code. 基本上你想改变裁剪矩形的长宽比,为此,只需注释掉代码中的以下几行即可。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM