Android 找不到配置的根目录,该根目录包含意图在片段中时的内容
互联网上有很多关于这个话题的问题和答案,但不幸的是,它并没有解决我的问题 每次我试图用相机拍摄片段中的录像图片时,都会出现一个错误。 我要保存这些文件的路径位于/data/data/com.example.testing/files中/* 我经常遇到的错误是:未能找到“包含/data/data/com.example.testing/files/test.jpg”和“未能找到包含/data/data/com.example.testing/files/test.mp4”的配置根目录 完整的错误日志:Android 找不到配置的根目录,该根目录包含意图在片段中时的内容,android,kotlin,mobile,android-camera,android-camera-intent,Android,Kotlin,Mobile,Android Camera,Android Camera Intent,互联网上有很多关于这个话题的问题和答案,但不幸的是,它并没有解决我的问题 每次我试图用相机拍摄片段中的录像图片时,都会出现一个错误。 我要保存这些文件的路径位于/data/data/com.example.testing/files中/* 我经常遇到的错误是:未能找到“包含/data/data/com.example.testing/files/test.jpg”和“未能找到包含/data/data/com.example.testing/files/test.mp4”的配置根目录 完整的错误日
2020-12-16 00:14:32.093 6473-6473/com.example.testing E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.testing, PID: 6473
java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.example.testing/files/test.jpg
at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:744)
at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
at com.example.testing.fragments.InvitesCheck.capturePhoto(InvitesCheck.kt:141)
at com.example.testing.fragments.InvitesCheck.access$capturePhoto(InvitesCheck.kt:32)
at com.example.testing.fragments.InvitesCheck$onViewCreated$1.onClick(InvitesCheck.kt:53)
at android.view.View.performClick(View.java:7192)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View.performClickInternal(View.java:7166)
at android.view.View.access$3500(View.java:824)
at android.view.View$PerformClick.run(View.java:27592)
at android.os.Handler.handleCallback(Handler.java:888)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:8178)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
我尝试了不同的设置,但不幸的是没有运气
这是我的舱单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testing">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Testing">
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
</application>
</manifest>
xml/provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external_files" path="."/>
</paths>
我做错了什么
提前感谢您。阅读本文,您似乎应该使用Context.getExternalFilesDir()
而不是Context.getFilesDir()`来创建要写入的文件
结果会是这样的:
private fun capturePhoto(){
val file=file(requireContext().getExternalFilesDir(null).path,“test.jpg”)
val uri=FileProvider.getUriForFile(require(),BuildConfig.APPLICATION_ID.toString()+“.provider”,文件)
val intent=intent(MediaStore.ACTION\u IMAGE\u捕获)
intent.addFlags(intent.FLAG\授予\读取\ URI\权限)
intent.putExtra(MediaStore.EXTRA_输出,uri)
startActivityForResult(意图、请求代码)
}
奖励:如果你打算制作一个面向媒体的应用程序,我建议你看看这个课程 你能发布一个更完整的堆栈跟踪吗?另外,您是否在清单中使用了正确的权限?.filesDir与消息中的外部路径不匹配。@blackapps如果我正确,我应该授予从根目录(.)及以上写入文件的权限?filesDir将与外部_文件相同,对吗?@IvanGarza我应该在清单中拥有正确的权限,我更新了主题并粘贴了清单和错误日志。我已经尝试过不同的方法,但到目前为止运气不好。非常感谢你,它很有魅力。/data/data/com.example.testing/files/和/storage/emulated/0/Android/data/com.example.testing/files/之间有什么大的区别吗?我相信您以前使用的位置是用于临时资产的,它主要由您的应用程序内部使用,例如下载图像或诸如此类。很高兴它成功了!我相信您以前使用的位置是用于临时资产的,它主要由您的应用程序内部使用,例如,下载图像或诸如此类。很高兴它成功了!
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external_files" path="."/>
</paths>