android KitKat image Crop

允我心安 提交于 2019-12-02 08:40:10

this code works successfully for me.try it

private static final int CAMERA_REQUEST = 1;
public static final int MEDIA_TYPE_IMAGE = 1;
final int PIC_CROP = 12;
Button btnCamera;
ImageView iv;
Uri picUri;
static File mediaFile, sendFile;



btnCamera.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            onImgProfile();
        }
    });



void onImgProfile() {
    Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    picUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

    captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, picUri);

    startActivityForResult(captureIntent, CAMERA_REQUEST);
}


private Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}

private File getOutputMediaFile(int type) {

    File mediaStorageDir = new File(
            Environment.getExternalStorageDirectory(), "MyCameraApp");

    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
            .format(new Date());

    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFile;
}



    @SuppressWarnings("unchecked")
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CAMERA_REQUEST) {
        // if (Build.VERSION.SDK_INT < 19) {
        try {
            if (mediaFile.exists()) {
                performCrop();
                // new SavePhotoData().execute();
            }

        } catch (Exception e) {
            // TODO: handle exception

        }
        // }

    } else if (requestCode == 11) {

        try {
            picUri = data.getData();
            Log.i("uri", "" + picUri);
            performCrop();
        } catch (Exception e) {
            // TODO: handle exception

        }

    } else if (requestCode == PIC_CROP) {
        // get the returned data

        try {
            Bundle extras = data.getExtras();

            // get the cropped bitmap
            Bitmap thePic = extras.getParcelable("data");
            // retrieve a reference to the ImageView

            // display the returned cropped image
            iv.setImageBitmap(thePic);

            File mediaStorageDir = new File(
                    Environment.getExternalStorageDirectory(),
                    "MyCameraApp");

            if (!mediaStorageDir.exists()) {
                if (!mediaStorageDir.mkdirs()) {
                    Log.d("MyCameraApp", "failed to create directory");

                }
            }

            // Create a media file name
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                    .format(new Date());

            sendFile = new File(mediaStorageDir.getPath() + File.separator
                    + "IMG_" + timeStamp + ".png");

            FileOutputStream fOut = new FileOutputStream(sendFile);

            thePic.compress(Bitmap.CompressFormat.PNG, 85, fOut);
            fOut.flush();
            fOut.close();

        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    if (resultCode == 3) {
        Bundle b = data.getExtras();
        b.getString("msg");
    }
};



private void performCrop() {
    // take care of exceptions
    try {
        // call the standard crop action intent (the user device may not
        // support it)
        try {
            Intent cropIntent = new Intent("com.android.camera.action.CROP");
            // indicate image type and Uri
            cropIntent.setDataAndType(picUri, "image/*");
            // set crop properties
            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", 256);
            cropIntent.putExtra("outputY", 256);
            // retrieve data on return
            cropIntent.putExtra("return-data", true);
            // start the activity - we handle returning in onActivityResult
            startActivityForResult(cropIntent, PIC_CROP);
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    // respond to users whose devices do not support the crop action
    catch (ActivityNotFoundException anfe) {
        // display an error message
        String errorMessage = "Whoops - your device doesn't support the crop action!";
        Toast toast = Toast.makeText(getApplicationContext(), errorMessage,
                Toast.LENGTH_SHORT);
        toast.show();
    }
}

this code works for not only kitkat but all cersion of android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!