1

There is no OK button displayed when I capture an image from the camera. If I press the back button, The control goes to the onActivityResult but the resultCode is not OK.

The camera opens fine and I can see the files being created in the folder that I specified, but their size is 0, maybe because the app has not saved the captured image yet.

BTW, the device's Android version is Pie. The app works on older versions (Kitkat and Marshmallow). Haven't tried it on Nougat and Oreo and went straight to Pie.

Here is my code:

public void launchCamera() {
    String[] permissions = new String[] {
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA
    };
    boolean hasPermission = (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
    if (!hasPermission) {
        //ask permission
        requestPermissions(permissions, REQUEST_WRITE_STORAGE);
    }
    else {
        hasPermission = (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
        if (!hasPermission) {
            requestPermissions(permissions, REQUEST_WRITE_STORAGE);
        }
        else {
            hasPermission = (checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED);
            if (!hasPermission) {
                requestPermissions(permissions, REQUEST_WRITE_STORAGE);
            }
            else {
                cameraUsePermitted();
            }
        }
    }
}

The onRequestPermission handler

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case 1:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                launchCamera();
            }
            else {
                Utils.toast(getApplicationContext(),
                        "App must be granted permission to use the camera.");
            }
            break;
    }
}

This launches the camera

private void cameraUsePermitted() {
    // the intent is my camera
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    //getting uri of the file
    if (Build.VERSION.SDK_INT < 24) {
        filenameOfCapturedImage = Uri.fromFile(getFile());
        Utils.log("FILENAME", "1");
    }
    else {
        filenameOfCapturedImage = Uri.parse(getFile().getPath());
        Utils.log("FILENAME", "2");
    }

    Utils.log("FILENAME", filenameOfCapturedImage.getPath());

    //Setting the file Uri to my photo
    intent.putExtra(MediaStore.EXTRA_OUTPUT, filenameOfCapturedImage);

    if(intent.resolveActivity(getPackageManager())!=null)
    {
        startActivityForResult(intent, PICTURE_RESULT);
    }
}

And the onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case PICTURE_RESULT:
        Utils.log("PICTURE", "1");
        if(resultCode == RESULT_OK) {
            Utils.log("PICTURE", "2");
            ReturnHeaderDisplay header = getCurrentHeader();
            String filename= filenameOfCapturedImage.getPath();
            long newId;
            newId = ReturnImages.addImage(getApplicationContext(),                      
                    header.getReturnId(), 
                    filename);

            if (newId >= 0) {

                ReturnImages.saveImage(getApplicationContext(), (int)newId, filename, header.getReturnId(), header.getCustomerName());

                returnImages.add(ReturnImages.loadReturnImageById(getApplicationContext(), (int)newId));

                addToReceipt(returnImages.get(returnImages.size()-1));
            }
            else {
                AppMessages.showMessage(ReturnActivity.this, 
                        "Unable to save image to database.", 
                        "Image Not Saved", 
                        AppMessages.MESSAGE_INFO, 
                        null);
            }

        }
        else {
            Utils.log("PICTURE", "3");
        }
        break;
    }        
}

the getFile() method

private File getFile() {
    File folder = Environment.getExternalStoragePublicDirectory(SD_CARD_IMAGE_PATH);

    //if it doesn't exist the folder will be created
    if (!folder.exists()) {
        folder.mkdirs();
    }

    String imageFileName = "RETURN_" + UUID.randomUUID();
    File image_file = null;

    try {
        image_file = File.createTempFile(imageFileName, ".jpg", folder);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return image_file;
}
F0r3v3r-A-N00b
  • 2,903
  • 4
  • 26
  • 36
  • 1
    I would guess that your camera app hides that button because you're sending it a malformed `Uri` – one without a scheme, or regular format – so it doesn't have a valid save target. `Uri.parse()` is very lenient, in that it won't throw an Exception it you pass it an invalid URI. I'd assume that you did that on 24+ to avoid a `FileUriExposedException`, but that's not how to do it. You need to implement a [`FileProvider`](https://developer.android.com/reference/android/support/v4/content/FileProvider), and get your `Uri` with that. – Mike M. Sep 19 '19 at 08:43
  • 1
    I would also mention that you do not need the `CAMERA` permission to take a picture with an external app, but that's not really related to the current issue. – Mike M. Sep 19 '19 at 08:45
  • 1
    @Mike M. Thanks. You're right. It was the FileUriExposedException that lead me to the code above. I followed [this](https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed) and it now works. – F0r3v3r-A-N00b Sep 19 '19 at 09:04
  • Cool. No problem. I almost linked you to that answer instead of the documentation, but decided otherwise, because the answer uses a `FileProvider` subclass, which is not really necessary for a basic, single `` setup. You can just use the `FileProvider` class directly, as mentioned in [this comment there](https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed#comment83526140_38858040). Anyhoo, glad you got it working. Cheers! – Mike M. Sep 19 '19 at 09:29

0 Answers0