如何从图库中裁剪图像并在imageview中设置

时间:2019-02-12 06:54:52

标签: android imageview

我正在从图库中裁剪图像,但是无法在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" />

2 个答案:

答案 0 :(得分:0)

    @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的结果,而不是基于cameraIntent的结果。

答案 1 :(得分:0)

 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.+'