在将图像设置为imageView之前的Android裁剪活动

时间:2016-03-30 07:47:58

标签: android android-activity android-imageview android-bitmap

请帮助,我想在将drawable设置为imageView之前进行图像裁剪活动。我想将裁剪的图像设置为imageView,selb是相同的..这是我的代码

   public void picselect(View view) {

    Toast.makeText(this, "Select a Picture", Toast.LENGTH_SHORT).show();
        //pic select intent
    Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, RESULT_LOAD_IMAGE);
}

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

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        /***
        *   i wish launch a crop activity then set cropped image to the imageView
        ***/
        //setting image to imageView
        imageView = (ImageView) findViewById(R.id.imageView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        //setting image to selb
        ImageView selb = (ImageView) findViewById(R.id.contimg);
        selb.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    }
}

请快点帮忙。感谢。

3 个答案:

答案 0 :(得分:0)

请参阅thisthis了解裁剪图片并设置为Imageview

答案 1 :(得分:0)

试试吧!

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

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
    Uri selectedImage = data.getData();
    ImageView selb = (ImageView) findViewById(R.id.contimg);
   selb.setImageUri(selectedImage);
}

}

答案 2 :(得分:0)

这是解决方案。谢谢大家的支持。它现在很完美。

public void picselect(View view) {

    Toast.makeText(this, "Select a pic", Toast.LENGTH_SHORT).show();
    Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, RESULT_LOAD_IMAGE);
}

@Override
protected void onActivityResult(int reqC, int resC, Intent data) {
    if (resC == RESULT_OK) {
        if (reqC == RESULT_LOAD_IMAGE) {
            picUri = data.getData();
            performCrop();
        } else if (reqC == PIC_CROP) {
            Bundle extras = data.getExtras();
            Bitmap thePic = extras.getParcelable("data");
            ImageView picv = (ImageView) findViewById(R.id.imageView);
            picv.setImageBitmap(thePic);
            imageView = (ImageView) findViewById(R.id.contimg);
            imageView.setImageBitmap(thePic);

        }
    }
}

private void performCrop() {
    try {
        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        cropIntent.setDataAndType(picUri, "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", 200);
        cropIntent.putExtra("outputY", 200);
        // retrieve data on return
        cropIntent.putExtra("return-data", true);
        // start the activity - we handle returning in onActivityResult
        startActivityForResult(cropIntent, PIC_CROP);
    } catch (ActivityNotFoundException anfe) {
        Toast t = Toast.makeText(this, "ANFE", Toast.LENGTH_SHORT);
        t.show();
    }
}