0

I'm trying to include functionality in an app for taking photos. The camera activity comes up, I can take a photo and tap the checkmark for using the photo just taken, but the app never receives it because onActivityResult is never called.

Here is the code that activates the camera activity:

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }

REQUEST_IMAGE_CAPTURE is set to 1.

The onActivityResult method that is never called looks like this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   ...
}

The app has only one activity, the main activity. I have tried different settings in the manifest, with no luck.

Richard G. Nielsen
  • 1,221
  • 10
  • 13

2 Answers2

0

As noted here you have to add this in the manifest.

Once this is added:

<uses-feature android:name="android.hardware.camera"
                  android:required="true" />

After you took the photo and accept it, the callback is called

Luca Nicoletti
  • 2,265
  • 2
  • 18
  • 32
  • @RichardG.Nielsen than there's something else wrong in your code. I copy-paster your code in a new project, new activity and it's working fine. – Luca Nicoletti Jun 18 '18 at 12:18
0
<uses-feature
    android:name="android.hardware.camera"
    android:required="false"/>


    private Uri mCapturedImageURI;

    ContentValues values = new ContentValues();
                        values.put(MediaStore.Images.Media.TITLE, System.currentTimeMillis() + "PRESCRIPTION");
                        mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                        Intent intentPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intentPicture.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
                        startActivityForResult(intentPicture, 1);

     public String getCameraPathFromURI(Uri contentUri) {
            try {
                String[] proj = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_index);
            } catch (Exception e) {
                return "";
            }
        }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
                case 1:
                if (resultCode == RESULT_OK) {
                    if (mCapturedImageURI != null) {
                        realImagePath = getCameraPathFromURI(mCapturedImageURI);

                    }
                }
                break;
    }
Mohsinali
  • 543
  • 7
  • 14
  • I tried you code, but it failed with java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=19464, uid=10076 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission() So I guess I need to add a permission in the manifest for this. – Richard G. Nielsen Jun 18 '18 at 09:25