Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

android - pick image from gallery and crop it with sizes larger than 500*500

I want to select image from gallery and crop it with 800*600 size, but with sizes larger than 500*500 it is not working!! how can I do it?

my code is as below:

public void showFileChooser() {
    Intent imageDownload = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    imageDownload.putExtra("crop", "true");
    imageDownload.putExtra("aspectX", 4);
    imageDownload.putExtra("aspectY", 3);
    imageDownload.putExtra("outputX", 800);
    imageDownload.putExtra("outputY", 600);
    imageDownload.putExtra("return-data", true);
    startActivityForResult(imageDownload, 2);
}

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2 && resultCode == RESULT_OK && null != data) {
            Bundle extras = data.getExtras();
                bitmap1 = extras.getParcelable("data");
                imageView1.setImageBitmap(bitmap1);
        }

}
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Try this, it works for me, Hope it'll help you too....

1 - Choose image from gallery

Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            intent.setType("image/*");
            startActivityForResult(Intent.createChooser(intent, "Select File"),Util.REQUEST_GALLERY);

2 - Crop image in onActivityResult as below

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == getActivity().RESULT_OK) {
        switch (requestCode) {
            case Util.REQUEST_GALLERY:
                try {
                    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) && !Environment.getExternalStorageState().equals(
                            Environment.MEDIA_MOUNTED_READ_ONLY)) {
                        File file = new File(Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()
                                + File.separator+ ".MyImages"+ File.separator+ "picture").getPath());
                        if (!file.exists()) {
                            file.mkdirs();
                        }

                        selectedPath1 = File.createTempFile("myImages"+ new SimpleDateFormat("ddMMyyHHmmss",Locale.US).format(new Date()),".jpg", file).toString();
                        croppedImageUri = Uri.fromFile(new File(selectedPath1));

                        Intent intent = new Intent("com.android.camera.action.CROP");
                        intent.setDataAndType(data.getData(), "image/*");
                        intent.putExtra("outputX", 700); // pass width
                        intent.putExtra("outputY", 700); // pass height
                        intent.putExtra("aspectX", 1);
                        intent.putExtra("aspectY", 1);
                        intent.putExtra("scale", true);
                        intent.putExtra("noFaceDetection", true);
                        intent.putExtra("output", croppedImageUri);
                        startActivityForResult(intent, Util.REQUEST_CROP_IMAGE);
                    } else {
                        Toast.show(getActivity(), "Please insert memory card to take pictures and make sure it is writeable");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

                case Util.REQUEST_CROP_IMAGE:
                     Logg.e(getClass().getSimpleName(), "Profile_Pic ===== " + selectedPath1);
                     imgProfile.setImageURI(Uri.parse("file://" + croppedImageUri));
                break;

                default:
                break;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...