我正在尝试使用ACTION_IMAGE_CAPTURE意图捕获图像,然后将其保存到/data/data/**/app_profile/profile_picture.jpg 我用以下代码调用该意图:
val cw = ContextWrapper(this@UserActivity.applicationContext)
val dir:File = cw.getDir("profile", Context.MODE_PRIVATE)
output = File(dir, "/profile_picture.jpg")
intent = Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE)
**val newPhotoUri = FileProvider.getUriForFile(applicationContext, "$packageName.fileprovider", output)**
intent.putExtra(MediaStore.EXTRA_OUTPUT, newPhotoUri)
startActivityForResult(intent, REQUEST_CAMERA)
Log.e(TAG, "output: ${output.absolutePath}") ///data/user/0/**/app_profile/profile_picture.jpg
但是我在FileProvider.getUriForFile()
上遇到了以下崩溃
java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/**/app_profile/profile_picture.jpg
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:739)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:418)
at **.UserActivity.takePhoto(UserActivity.kt:147)
我在清单中设置了以下内容:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="**.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
我的filepaths.xml:
<paths>
<files-path name="app_profile" path="." />
</paths>
我已经尝试过
<files-path name="app_profile" path="app_profile/" />
和许多其他组合。 有人可以指导我吗?
答案 0 :(得分:0)
我自己修复了。而不是
val dir:File = cw.getDir("profile", Context.MODE_PRIVATE)
我不得不做
val dir:File = cw.filesDir
哪个将返回正确的/ files /目录。 现在一切正常。
答案 1 :(得分:0)
我检查了FileProvider
的来源,发现了一个未记录的解决方案:
<root-path name="root_files" path="." />
这显然将授予对您的应用程序可以访问的每个目录的访问权限。可能不是最安全的方法(尤其是如果您已经拥有WRITE_EXTERNAL_STORAGE
),但是它将允许您共享位于context.getDir()
下的文件,否则这是不可能的。
如果可能的话,更好的解决方案是将文件复制或移动到context.getFilesDir()
下。