2015-11-11 30 views
36

我正在试图让相机应用程序将输出存储到我的内部存储器。我也明白,第三方应用程序无法访问我的应用程序但是的内部存储,我们可以通过FileProvider公开内部目录。我按照这里的指南:FileProvider不能使用相机

我以下列方式指定我AndroidManifest.xml

<?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> 

/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> 

这是我如何创建和调用摄像头意图:

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

但这只会于Nexus /氰设备一起工作。三星相机应用程序可能会使用不同的包名称。

回答

91
  1. 请不要试图把exported="true"pskink指出,您的应用程序会崩溃,它加载的时刻。 FileProvider并不意味着在这种状态下工作。

  2. 尝试intent.addFlags解决方案通过CommonsWare。不工作。可能只会与ACTION_SEND类型的意图。我的答案是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); 
    } 
    
+3

非常令人满意的破解:) –

+26

'addFlags()'适用于Android 5.0+。在较旧的设备上,您的方法是必需的。有关示例,请参阅https://github.com/commonsguy/cw-omnibus/tree/master/Camera/FileProvider。 – CommonsWare

+2

我在这里有一个疑问。我很抱歉我对Android的知识很少,但我很困惑。在启动相机之前,我将目录设置为“Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES”)。我还需要使用FileProvider还是仅用于存储在私人应用程序区域中的图像? – AndroidGuy

0

我有一个类似的问题,但后来我发现有在我的代码一个错字。 错字位于AndroidManifest.xml中,正好位于“权限”行的标记中。

对于我来说,

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

不工作(当我打电话的相机例如,应用程序崩溃),但如果我删除“文件”并替换该字符串如下:

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

我的应用程序运行平稳(应用程序拍摄照片,存储它,并将预览返回给客户端应用程序中的活动)。

您的应用程序是否也有相同的问题?