【问题标题】:how to crop image from gallery and set in imageview如何从图库中裁剪图像并在 imageview 中设置
【发布时间】:2023-04-10 10:40:02
【问题描述】:

我正在从图库中裁剪图像,但无法在imageView 中设置该裁剪图像,最终图像设置为默认图像我的意思是没有裁剪所以如何在ImageView 中设置裁剪图像

这是来自图库的图片选择器

 newPostImg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            galIntent=new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(Intent.createChooser(galIntent,"select image"),2);
        }
    });

这是用于裁剪图像

private void CropImage() {
    try {
        CropIntent = new Intent("com.android.camera.action.CROP");
        CropIntent.setDataAndType(postImageUri,"image/*");
        CropIntent.putExtra("crop","true");
        CropIntent.putExtra("outputX",180);
        CropIntent.putExtra("outputY",180);
        CropIntent.putExtra("aspectX",3);
        CropIntent.putExtra("aspectY",4);
        CropIntent.putExtra("scaleUpIfNeeded",true);
        CropIntent.putExtra("return-data",true);

        startActivityForResult(CropIntent,1);

    }
    catch (ActivityNotFoundException ex)
    {

    }
}

这是 onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {


    if (requestCode == 0&&resultCode ==RESULT_OK)
        CropImage();
        if (requestCode==2){
            if (data!=null){
                postImageUri=data.getData();
                CropImage();
                newPostImg.setImageURI(postImageUri);

            }
        }

}

这是我的许可

 <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA"/>

这是 imageView xml

 <ImageView
        android:layout_margin="10dp"
        android:id="@+id/newPostImg"
        android:layout_width="match_parent"
        android:layout_height="250dp"
       android:src="@drawable/default1" />

【问题讨论】:

  • 你没有使用裁剪意图的结果。此外,设备完全不需要com.android.camera.action.CROP 动作处理程序,所以你不应该依赖它
  • 处理 requestCode = 1 的结果。
  • 使用this进行图像裁剪。
  • @AbhayKoradiya..in 哪个请求代码必须为 1?

标签: android imageview


【解决方案1】:
 Uri imageUri = Uri.parse(mCurrentPhotoPath);
  Intent intent = new Intent(mContext, Crop_Activity.class);
  intent.putExtra("imageUri", imageUri.toString());
  startActivityForResult(intent, 105);

这是作物活动的代码

public class Crop_Activity extends BaseActivity implements CropImageView.OnSetImageUriCompleteListener, CropImageView.OnCropImageCompleteListener, View.OnClickListener {

    private CropImageView mCropImageView;
    private ProgressDialog dialog;
    int type;
    private Uri imageUri;
    private Bitmap cropped;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.crop_activity);

        setScreenName("Crop Image screen");

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            imageUri = Uri.parse(getIntent().getStringExtra("imageUri"));
        }

        //====Initialize Here====//
        init();

        //====Set CropImageView Here====//
        setCropImageView();

    }

    void init() {
        dialog = new ProgressDialog(Crop_Activity.this);
        dialog.setCancelable(false);
        dialog.setMessage("Cropping Image...");
        Func.set_title_to_actionbar("Crop Image", "", "", Crop_Activity.this, (Toolbar) findViewById(R.id.toolbar), true, 8, 8, Crop_Activity.this);
        mCropImageView = (CropImageView) findViewById(R.id.CropImageView);
    }

    void setCropImageView() {
        mCropImageView.setOnSetImageUriCompleteListener(this);
        mCropImageView.setOnCropImageCompleteListener(this);
        mCropImageView.setFixedAspectRatio(true);
        mCropImageView.setImageUriAsync(imageUri);
        mCropImageView.setShowProgressBar(false);
        mCropImageView.setFixedAspectRatio(true);
        mCropImageView.setAspectRatio(10, 10);
    }

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        getMenuInflater().inflate(R.menu.menu_crop, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            onBackPressed();
            finish();
            return true;
        }
        if (id == R.id.btn_ok) {
            mCropImageView.getCroppedImageAsync();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onSetImageUriComplete(CropImageView view, Uri uri, Exception error) {
    }

    @Override
    public void onClick(View view) {

    }

    class CropingImageAsync extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            Constants.Photo = cropped;
            Intent i = new Intent();
            setResult(RESULT_OK, i);
            finish();
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            dialog.dismiss();
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
            finish();
        }
    }

    @Override
    public void onCropImageComplete(CropImageView view, CropImageView.CropResult result) {
        handleCropResult(result);
    }

    private void handleCropResult(CropImageView.CropResult result) {
        if (result.getError() == null) {
            Bitmap bitmap = result.getBitmap();
            if (bitmap != null) {
                cropped = bitmap;
                if (cropped != null) {
                    new CropingImageAsync().execute();
                }
            }
        } else {
            Log.e("AIC", "Failed to crop image", result.getError());
        }
    }
}

这是作物活动的 XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:id="@+id/line1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.theartofdev.edmodo.cropper.CropImageView
        android:id="@+id/CropImageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:cropInitialCropWindowPaddingRatio="0" />

</LinearLayout>

裁剪的依赖性

compile 'com.theartofdev.edmodo:android-image-cropper:2.3.+'

【讨论】:

    【解决方案2】:
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
    
            if (resultCode == RESULT_OK) {
    
                if (requestCode == 2) {
                    if (data != null) {
                        postImageUri = data.getData();
                        CropImage();
                    }
                } else if (requestCode == 1) {
                    Bundle extras = data.getExtras();
                    Bitmap bitmap= extras.getParcelable("data");
                    newPostImg.setImageBitmap(bitmap);
                }
            }
        }
    
    
    }
    

    您应该在 CropIntent 的结果上设置 Image,而不是在 cameraIntent 的结果上。

    【讨论】:

    • 当我这样做时..结果也一样。我的意思是..图像设置为默认图像而不是裁剪图像
    • newPostImg.setImageURI( data.getData()); 这就是它的工作原理
    • 我已经更新了我的答案,它现在应该可以工作了。
    • thanku alll..但我确实通过@AbhayKoradiya 使用第三方库进行了裁剪。
    猜你喜欢
    • 2015-07-08
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    • 1970-01-01
    • 2015-11-13
    相关资源
    最近更新 更多