Content uri crashes camera on Android KitKat

喜夏-厌秋 提交于 2020-01-29 05:39:04

问题


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?


回答1:


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.




回答2:


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);
    }


来源:https://stackoverflow.com/questions/40087944/content-uri-crashes-camera-on-android-kitkat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!