当前位置: 首页 > 知识库问答 >
问题:

FileProvider无法使用Camera

张璞
2023-03-14

我正在尝试使相机应用程序存储输出到我的内部存储。我还知道第三方应用程序不能访问我的应用程序的内部存储,但我们可以通过fileprovider公开内部目录来做到这一点。我在这里遵循了指南:

  • 相机意图不保存照片
  • https://developer.android.com/reference/android/support/v4/content/fileprovider.html
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.stackoverflow.test.camerawithfileprovider" >

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

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.stackoverflow.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>
<?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>
private static final int CAMERA_REQUEST_CODE = 5000;
private static final String CAMERA_FP_AUTHORITY = "com.stackoverflow.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.stackoverflow.test.camerawithfileprovider D/YouQi: Image URI Passing to Camera: content://com.stackoverflow.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$8.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
);

共有1个答案

冷英博
2023-03-14

>

  • 请不要尝试将exported=“true”pskink所指出的,您的应用程序将在加载时崩溃。FileProvider不能在此状态下工作。

    通过commonsware尝试了intent.addflags解决方案。不管用。可能只适用于action_send类型的意图。

    我在这篇帖子里找到了答案。显然,手动授予的方式是唯一的解决办法。但我们总是可以循环遍历候选包列表并将权限授予所有这些包。

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

  •  类似资料:
    • 从外部应用程序,我想这样做,以访问文件提供商的内部存储上的文件: 是我在其他(fileprovider)应用程序的清单xml中指定的权限字符串。 和: 该文件存在于内部FP的存储器中(files)/path/to/file下。txt 但我总是得到IllegalArgumentException no root等。这不是默认的Android FileProvider的用途吗?我是否误解了它的用途?例

    • 由于我已使用FileProvider将我的应用程序迁移到Android N,所以我无法在手机的图库中看到我的图片(即使版本低于Android N)。所以我创建了一个小测试应用程序来调试它,但没有成功。 在我的舱单中 在file\u路径中。xml 我做了一个小活动,显示上次拍摄的照片,以确保URI是正确的: 我的照片在我的应用程序和我的文件浏览器中,如预期的那样,但我在照片库中看不到它。我尝试使用F

    • 我正在用FileProvider在Android牛轧糖上拍照,这是我的代码 file_paths.xml: 编辑丢失的文件#createNewFile();

    • 跟随https://developer.android.com/training/secure-file-sharing/index.html,并能够共享文件在内部目录(/data/data/pack/files/xxx/)的应用程序到客户端应用程序使用file提供程序。 如何将资产文件夹(而不是内部目录)中的文件共享到客户端应用程序。 谢谢

    • 我的应用程序具有“附加文件”、“拍照”、“拍摄视频”等功能。我正在将文件Uri传递给一个新的Intent,但在Nougat中得到了FileUriExposedException。因此已修改代码以使用FileProvider。我的Content Uri很好,但当我尝试读取或上传文件/图片/视频时,我java.io.FileNotFoundException。我设置权限错误吗?还是需要以其他方式设置权

    • 但是,执行这一行给了我(为了保护无辜者而改变了名字): 应用程序A在清单文件中有以下内容: filePaths.xml具有: 文件最终的实际路径是: 在这一点上,我被难住了。我不知道为什么不能授予我在同一个应用程序中创建的文件的权限。所以我的问题是:为什么我会得到这个异常,我如何才能成功地授予应用程序B的权限?