【问题标题】:Crop Image from gallery and set it as my imageview background Android从图库中裁剪图像并将其设置为我的 imageview 背景 Android
【发布时间】:2016-01-18 17:08:38
【问题描述】:

我解决了这个问题,这里是在(android:select image from gallery then crop that and show in an imageviewstackoverflow 中的帖子的帮助下更新的代码(感谢 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();
                }
            }   

}

【问题讨论】:

  • 请说明您面临的问题。
  • 嗨 Atifail,实际上图像正在被裁剪,但 imageview 的背景反映的是原始的 centercrop 图像。我的意思是那些我从图库中选择图像然后裁剪它,当我点击保存这个裁剪的图像不反映画廊中的原始图像,仅位于图像视图中。

标签: android imageview


【解决方案1】:

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

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

【讨论】:

  • 嗨 Atifail,实际上图像正在被裁剪,但 imageview 的背景反映的是原始 centercrop 图像。我的意思是那些我从图库中选择图像然后裁剪它,当我点击保存这个裁剪图像不反映画廊中的原始图像,仅位于图像视图中。
  • 您的意思是裁剪图像没有设置为 imageView 还是 imageview 显示原始图像和裁剪图像。???
  • 我解决了这个问题,实际上我忘记通过意图获取图片路径字符串。我正在为其他人添加代码sn-p。
猜你喜欢
  • 2015-11-13
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 2015-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-06
相关资源
最近更新 更多