user3769641
user3769641

Reputation: 1

Camera and gallery crop options

I am using camera option in my app but it works some devices only. Also I need crop options.

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == GALLERY_CODE && resultCode == RESULT_OK && null != data) {

        mImageCaptureUri = data.getData();
        System.out.println("Gallery Image URI : "+mImageCaptureUri);
        CropingIMG();

    }
}

Upvotes: 0

Views: 59

Answers (1)

Selva Ganesh
Selva Ganesh

Reputation: 151

To fix camera issue, I have used following methods

  1. CameraIntentHelper

  2. onSaveInstanceState

  3. onRestoreInstanceState

  4. onActivityResult

    private void selectImage() {
    
    final CharSequence[] options = {"Take Photo", "Choose from Gallery", "Cancel"};
    AlertDialog.Builder builder = new AlertDialog.Builder(PhotoSuiteActivity.this);
    builder.setTitle("Add Photo!");
    builder.setItems(options, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
    
            if (options[item].equals("Take Photo")) {
                if (mCameraIntentHelper != null) {
                    mCameraIntentHelper.startCameraIntent();
                } else {
                    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/picture.png";
                    File imageFile = new File(imageFilePath);
                    picUri = Uri.fromFile(imageFile); // convert path to Uri
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, picUri);
                    startActivityForResult(takePictureIntent, CAMERA_CAPTURE);
                }
            } else if (options[item].equals("Choose from Gallery")) {
                Toast.makeText(PhotoSuiteActivity.this, "Not yet Ready...!", Toast.LENGTH_SHORT).show();
                Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                // Start the Intent
                startActivityForResult(galleryIntent, PICK_IMAGE_REQUEST);
            } else if (options[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
    
     }
    
     private void setupCameraIntentHelper() {
        mCameraIntentHelper = new CameraIntentHelper(this, new CameraIntentHelperCallback() {
            @Override
            public void onPhotoUriFound(Date dateCameraIntentStarted, Uri photoUri, int rotateXDegrees) {
                messageView.setText(getString(R.string.activity_camera_intent_photo_uri_found) + photoUri.toString());
                mImageCaptureUri = photoUri;
                Bitmap photo = BitmapHelper.readBitmap(PhotoSuiteActivity.this, photoUri);
                if (photo != null) {
                    photo = BitmapHelper.shrinkBitmap(photo, 300, rotateXDegrees);
                    imageView.setImageBitmap(photo);
                    CropingIMG();
                }
            }
        @Override
        public void deletePhotoWithUri(Uri photoUri) {
            BitmapHelper.deleteImageWithUriIfExists(photoUri, PhotoSuiteActivity.this);
        }
    
        @Override
        public void onSdCardNotMounted() {
            Toast.makeText(getApplicationContext(), getString(R.string.error_sd_card_not_mounted), Toast.LENGTH_LONG).show();
        }
    
        @Override
        public void onCanceled() {
            Toast.makeText(getApplicationContext(), getString(R.string.warning_camera_intent_canceled), Toast.LENGTH_LONG).show();
        }
    
        @Override
        public void onCouldNotTakePhoto() {
            Toast.makeText(getApplicationContext(), getString(R.string.error_could_not_take_photo), Toast.LENGTH_LONG).show();
        }
    
        @Override
        public void onPhotoUriNotFound() {
            messageView.setText(getString(R.string.activity_camera_intent_photo_uri_not_found));
        }
    
        @Override
        public void logException(Exception e) {
            Toast.makeText(getApplicationContext(), getString(R.string.error_sth_went_wrong), Toast.LENGTH_LONG).show();
            Log.d(getClass().getName(), e.getMessage());
        }
    });
    }
    

Upvotes: 1

Related Questions