Search code examples
javaandroidandroid-camera

Could not capture image in Android


  • Device: OnePlus 6
  • Storage: 8GB RAM + 128GB ROM
  • Android Version: 10
  • Oxygen OS Version: OP6_02_BETA_31

I use the below code capture image. The code is perfectly working in almost all devices except for the above-mentioned configuration.

The app is opening the camera, and I could not capture the image. With the default camera app, I was not getting any exception. Once the photo is captured, a tick mark button appears to confirm the image. But that button is not being clickable. So, I used a different camera app to capture the image. It returned "Failed to save photo" error in the camera app. No exceptions are thrown.

And I get this message "E/ContentProviderNative: onTransact error from {P:7857;U:10456}" in the console. Please assist me with this issue.

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    try {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            String appDirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + context.getString(R.string.app_name);
            appDir = new File(appDirPath);
            if (!appDir.exists()) {
                appDir.mkdirs();
            }

            mFileCaptured = new File(appDir, tag + ".jpg");

            mImageCaptureUri = FileProvider.getUriForFile(context,
                    context.getApplicationContext().getPackageName() + ".provider",
                    mFileCaptured);
        }
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
            intent.setClipData(ClipData.newRawUri("", mImageCaptureUri));
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
        intent.putExtra("return-data", true);
        context.startActivityForResult(intent, 10000);

    } catch (Exception e) {
        Crashlytics.logException(e);
        Crashlytics.log(e.getLocalizedMessage());
    }

Solution

  • I followed the implementation from https://developer.android.com/training/camera-deprecated/photobasics.

    Now, it's working for me.