How to use Camera Intent with Crop Intent both at

2019-09-13 06:32发布

问题:

I followed this to use camera from web-view..with upload,,,

So in that I am getting Default camera.. and I am capturing.. and Uploading...

So I want to Crop them...and Upload.. with Max 2 Megapixel resolution not more than 2Mp...

I followed this to use Crop...

I it Possible to use both intents together in web-view... and resolution should be low.. I mean after capture it should show crop... option... and then upload,,,

Can any one suggest me...

回答1:

private void pickImage() {
    AlertDialog.Builder builder = new AlertDialog.Builder(Create_event.this);
    builder.setTitle("Choose Image");
    builder.setMessage("Select Image");
    builder.setPositiveButton("Gallery",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent photoPickerIntent = new Intent(
                            Intent.ACTION_PICK);
                    photoPickerIntent.setType("image/*");
                    startActivityForResult(photoPickerIntent, 1);
                    dialog.dismiss();
                }
            });
    builder.setNegativeButton("Camera",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent cameraIntent = new Intent(
                            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(cameraIntent, 2);
                    dialog.dismiss();
                }
            });
    builder.show();
}

private Uri getTempUri() {
    return Uri.fromFile(getTempFile());
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == 2 || requestCode == 1) {
            try {
                Intent cropIntent = new Intent(
                        "com.android.camera.action.CROP");
                // indicate image type and Uri
                cropIntent.setDataAndType(data.getData(), "image/*");
                cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
                cropIntent.putExtra("outputFormat",
                        Bitmap.CompressFormat.JPEG.toString());
                // 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, 3);
            } catch (ActivityNotFoundException anfe) {
                // display an error message
                String errorMessage = "Whoops - your device doesn't support the crop action!";
                Toast toast = Toast.makeText(this, errorMessage,
                        Toast.LENGTH_SHORT);
                toast.show();
            }
        } else if (requestCode == 3) {
            try {
                Log.e("testing", "return data is  " + data.getData());

                String filePath = Environment.getExternalStorageDirectory()
                        + "/" + TEMP_PHOTO_FILE;
                System.out.println("path " + filePath);
                uImage = BitmapFactory.decodeFile(filePath);
                ByteArrayOutputStream bao = new ByteArrayOutputStream();
                uImage.compress(Bitmap.CompressFormat.PNG, 100, bao);
                ba = bao.toByteArray();
                ivcreateeventflyer.setImageBitmap(uImage);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

private static final String TEMP_PHOTO_FILE = "temporary_holder.jpg";

private File getTempFile() {

    if (Environment.getExternalStorageState().equals(
            Environment.MEDIA_MOUNTED)) {

        File file = new File(Environment.getExternalStorageDirectory(),
                TEMP_PHOTO_FILE);
        try {
            file.createNewFile();
        } catch (IOException e) {
        }

        return file;
    } else {

        return null;
    }
}

Use this code to take pic from camera or gallery with crop functionality. hope it may be help you