Android - 相機閃退,SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider

java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord ... that is not exported from....

這個是因為android7.0以上要shareFile權限問題,不能直接用File去接,要轉成Uri去使用

 

https://juejin.im/entry/586dbd798d6d8100586ac8e2

http://www.jianshu.com/p/358b5d791045

這兩條應該可以解決基本上的問題

本人實做OPPO R5 Android 4.4.4

在用intent呼叫camera時,要另外加上臨時權限

    try {
            Intent intent_camera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
//          fileUri = Uri.fromFile(getOutputMediaFile(1));
            fileUri = FileProvider.getUriForFile(appPublic, appPublic.getApplicationContext().getPackageName() + ".fileprovider",getOutputMediaFile(1));
            intent_camera.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

            //就是這段 花了我一整天的時間....
            List<ResolveInfo> resolvedIntentActivities = appPublic.getPackageManager()
                  .queryIntentActivities(intent_camera, PackageManager.MATCH_DEFAULT_ONLY);
            for (ResolveInfo resolvedIntentInfo : resolvedIntentActivities) {
               String packageName = resolvedIntentInfo.activityInfo.packageName;
               appPublic.grantUriPermission(packageName, fileUri, FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
            }

            this.appPublic.startActivityForResult(intent_camera, CAMERA);
            System.gc(); //提醒系統及時回收
         }catch(Exception e){
            e.printStackTrace();
         }