Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

java - Android intent with multiple option i.e , Pick image from gallary and capture image using front camera

After selecting/capturing the image i need to crop the image.

I already done this one.but the problem is when i am switching to the android latest version(kitkat) crop intent is not working properly.

My Code

    private void picPhoto() {
    Intent pickIntent = new Intent();
    if (Build.VERSION.SDK_INT < 19) {
        pickIntent.setType("image/jpeg");
        pickIntent.setAction(Intent.ACTION_GET_CONTENT);
        pickIntent.putExtra("crop", "true");
    } else {
        pickIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        pickIntent.addCategory(Intent.CATEGORY_OPENABLE);
        pickIntent.setType("image/jpeg");
        pickIntent.putExtra("crop", "true");
    }
    Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, getFileDirectory());
    takePhotoIntent.putExtra("android.intent.extras.CAMERA_FACING", 1);

    String pickTitle = "Select or take a new Picture";
    Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);

    chooserIntent.putExtra
            (
                    Intent.EXTRA_INITIAL_INTENTS,
                    new Intent[]{takePhotoIntent}
            );

    startActivityForResult(chooserIntent, PICK_IMAGE);
}

Can any one help with me an example ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I used this method to crop a pic which was working in kitkat too

@Override
    public void onActivityResult(int requestCode, int resultCode,
            final Intent data) {
 if (resultCode == Activity.RESULT_OK&&requestCode==1) {  

                     performCrop(image_uri);

}
}
    private void performCrop(Uri picUri) {

        thePic = null;

        // take care of exceptions
        try {
            // call the standard crop action intent (the user device may not
            // support it)

            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", 2);
            cropIntent.putExtra("aspectY", 1);
            // // // indicate output X and Y

            // retrieve data on return
            cropIntent.putExtra("return-data", true);
            // start the activity - we handle returning in onActivityResult
            startActivityForResult(cropIntent, 3);
        }
        // respond to users whose devices do not support the crop action
        catch (ActivityNotFoundException anfe) {
            Toast toast = Toast.makeText(getActivity(),
                    "This device doesn't support the crop action!",
                    Toast.LENGTH_SHORT);
            toast.show();
        } catch (OutOfMemoryError e) {
            System.out.println("out of memoryyy");
        } catch (Exception e) {

            Display display = getActivity().getWindowManager()
                    .getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            int width = size.x;
            int height = size.y;
            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", 2);
            cropIntent.putExtra("aspectY", 1);
            // // indicate output X and Y
            cropIntent.putExtra("outputX", width);
            cropIntent.putExtra("outputY", 200);
            // retrieve data on return
            cropIntent.putExtra("return-data", true);
            // start the activity - we handle returning in onActivityResult
            startActivityForResult(cropIntent, 3);
        }

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...