How to use the camera’s intent with the intent to crop as on one intent in Webview

I followed this to use the camera from web browsing .. with download ,,

So, I get the default camera .. and I capture .. and Uploading ...

So, I want to crop them ... and Download .. with a resolution of Max 2 megapixels no more than 2 MP ...

I used this one to use Crop ...

I can use both intentions together in a web view ... and the resolution should be low .. I mean that after capturing it should show crop ... option ... and then load ,,

Can anyone suggest me ...

0
source share
1 answer
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 display pic from a camera or gallery with cropping functionality. Hope this can help you.

0
source

Source: https://habr.com/ru/post/1630000/


All Articles