android - java.lang.IllegalArgumentException:未能找到包含 /data/data/ 的已配置根目录
问题描述
我正在尝试使用 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>
我的文件路径.xml:
<paths>
<files-path name="app_profile" path="." />
</paths>
我已经试过了
<files-path name="app_profile" path="app_profile/" />
以及许多其他组合。有人可以指导我吗?
解决方案
我自己修好了。而不是做
val dir:File = cw.getDir("profile", Context.MODE_PRIVATE)
我必须做
val dir:File = cw.filesDir
它返回正确的 /files/ 目录。现在一切正常。