3

I run into problem while using content uri and FileProvider on Android API 19 (Kitkat). Here's code that I use to open camera on device and record a video:

File file = new File(pathname);
Uri fileUri = FileProvider.getUriForFile(this, AUTHORITY_STRING, file);

Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, requestCode);

I've tested this code on API's 23-24 and it works just fine, but on API 19 camera closes with RESULT_CANCELED after I starting to take video in camera app. The same thing happening when I'm trying to take a picture with ACTION_IMAGE_CAPTURE action. I've tried to change FileProvider.getUriForFile() with Uri.fromFile(). This works perfectly on Kitkat, but I can't use it on Android 7. Why is Camera on Adnroid Kitkat doesn't want to work with content uri?

Yenn
  • 200
  • 2
  • 11

2 Answers2

17

maybe you can try this one.

    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT){
        fileUri = Uri.fromFile(mTmpFile);
    } else {
        fileUri = FileProvider.getUriForFile(getContext(),
                getContext().getPackageName()+".provider",mTmpFile);
    }
李志南
  • 181
  • 1
  • 5
3

Not every camera app will support content Uri values for EXTRA_OUTPUT, though they should. But, there are thousands of camera apps, and some percentage will fail on such a Uri. For example, until this summer, Google's own camera app did not support a content Uri for EXTRA_OUTPUT for ACTION_VIDEO_CAPTURE.

Either stop using ACTION_VIDEO_CAPTURE or live with unreliable results.

If you wish to try continuing to use file Uri values, drop your targetSdkVersion to 23 or lower, or disable the FileUriExposedException by changing the StrictMode configuration.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491