FileProvider 不适用于相机

FileProvider Not Working with Camera

我正在尝试让相机应用程序将输出存储到我的内部存储器中。我也知道第三方应用程序无法访问我的应用程序的内部存储 但是 我们可以通过 FileProvider 公开内部目录来访问。我已按照此处的指南进行操作:

我按以下方式指定我的 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.Whosebug.test.camerawithfileprovider" >

    <application>
        <activity
            ...
        </activity>

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.Whosebug.test.camerawithfileprovider.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true" >
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_provider_path" />
        </provider>
    </application>

</manifest>

我在 /res/xml/ 中创建了一个名为 file_provider_path.xml 的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="image_capture" path="public/" />
</paths>

这是我创建和调用 Camera Intent 的方式:

private static final int CAMERA_REQUEST_CODE = 5000;
private static final String CAMERA_FP_AUTHORITY = "com.Whosebug.test.camerawithfileprovider.fileprovider";

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

Context context = MainActivity.this;
File imagePath = new File(context.getFilesDir(), "public");
if (!imagePath.exists()) imagePath.mkdirs();
File newFile = new File(imagePath, "tmp.jpg");

Uri imageUri = FileProvider.getUriForFile(context, CAMERA_FP_AUTHORITY, newFile);
//context.grantUriPermission("com.google.android.GoogleCamera", imageUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
Log.d("YouQi", "Image URI Passing to Camera: " + imageUri.toString());

intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CAMERA_REQUEST_CODE);

当我运行申请时,我可以得到以下Logcat结果:

    11-11 20:07:39.581 18303-18303/com.Whosebug.test.camerawithfileprovider D/YouQi: Image URI Passing to Camera: content://com.Whosebug.test.camerawithfileprovider.fileprovider/image_capture/tmp.jpg

就在相机关闭的那一刻,我遇到了以下异常:

com.google.android.GoogleCamera E/AndroidRuntime: FATAL EXCEPTION: main
com.google.android.GoogleCamera E/AndroidRuntime: Process: com.google.android.GoogleCamera, PID: 19420
com.google.android.GoogleCamera E/AndroidRuntime: java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord{42e44f70 19420:com.google.android.GoogleCamera/u0a83} (pid=19420, uid=10083) that is not exported from uid 10312
com.google.android.GoogleCamera E/AndroidRuntime:     at android.os.Parcel.readException(Parcel.java:1465)
com.google.android.GoogleCamera E/AndroidRuntime:     at android.os.Parcel.readException(Parcel.java:1419)
com.google.android.GoogleCamera E/AndroidRuntime:     at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:2882)
com.google.android.GoogleCamera E/AndroidRuntime:     at android.app.ActivityThread.acquireProvider(ActivityThread.java:4544)
com.google.android.GoogleCamera E/AndroidRuntime:     at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2274)
com.google.android.GoogleCamera E/AndroidRuntime:     at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1425)
com.google.android.GoogleCamera E/AndroidRuntime:     at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:906)
com.google.android.GoogleCamera E/AndroidRuntime:     at android.content.ContentResolver.openOutputStream(ContentResolver.java:669)
com.google.android.GoogleCamera E/AndroidRuntime:     at android.content.ContentResolver.openOutputStream(ContentResolver.java:645)
com.google.android.GoogleCamera E/AndroidRuntime:     at com.android.camera.PhotoModule.onCaptureDone(PhotoModule.java:1281)
com.google.android.GoogleCamera E/AndroidRuntime:     at com.android.camera.PhotoModule.onClick(PhotoModule.java:593)
com.google.android.GoogleCamera E/AndroidRuntime:     at android.view.View.performClick(View.java:4445)
com.google.android.GoogleCamera E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:18446)
com.google.android.GoogleCamera E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:733)
com.google.android.GoogleCamera E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
com.google.android.GoogleCamera E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136)
com.google.android.GoogleCamera E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5146)
com.google.android.GoogleCamera E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
com.google.android.GoogleCamera E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
com.google.android.GoogleCamera E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
com.google.android.GoogleCamera E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
com.google.android.GoogleCamera E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

谁能帮忙解释一下为什么会出现这个问题?我已经为我的问题创建了一个示例项目:http://www.axzae.com/downloads/CameraWithFileProvider.zip

无论如何,如果我明确指定该应用程序将运行:

context.grantUriPermission(
    "com.google.android.GoogleCamera",
    imageUri,
    Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION
);

但这仅适用于 nexus/cyanogen 设备。三星相机应用程序可能使用不同的包名称。

  1. 请不要尝试按照 pskink 所指出的那样放置 exported="true",您的应用程序会在加载时崩溃。 FileProvider 不能在此状态下工作。

  2. 已尝试 intent.addFlags CommonsWare 的解决方案。不工作。可能只适用于 ACTION_SEND 种意图。

  3. 我找到了答案in this post。显然,手动授权方式是唯一的解决方案。但是我们总是可以遍历候选包列表并为所有包授予权限。

    List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    for (ResolveInfo resolveInfo : resInfoList) {
        String packageName = resolveInfo.activityInfo.packageName;
        context.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
    }
    

我遇到了类似的问题,但后来我发现我的代码中有错字。 打字错误在 AndroidManifest.xml 中,恰好在 "authorities".

行的标记中

对我来说,

android:authorities="com.Whosebug.test.camerawithfileprovider.fileprovider"

不起作用(当我调用相机实例时,应用程序崩溃)但是如果我删除 "file" 并按如下方式替换该字符串:

android:authorities="com.Whosebug.test.camerawithfileprovider.provider"

我的应用程序运行流畅(应用程序拍摄照片、存储照片并在客户端应用程序中将预览返回给 activity)。

您的应用是否也存在同样的问题?