【发布时间】:2018-07-17 00:44:09
【问题描述】:
我正在尝试使用 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)
我在 Manifest 中有以下设置:
<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)
标签: android kotlin android-fileprovider