当 Intent 在片段中时,找不到包含的已配置根目录
Failed to find configured root that contains when Intent is in fragment
网上有很多关于这个话题的问答,但不幸的是没有解决我的问题。
每次我尝试将相机用于我的 Fragment 中的视频录制图片时,我都会收到错误消息。
我要保存这些文件的路径在 /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.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.access00(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>
我正在使用以下函数调用相机:
private fun capturePhoto() {
val file = File(requireContext().filesDir.path, "test.jpg")
val uri = FileProvider.getUriForFile(requireActivity(), BuildConfig.APPLICATION_ID.toString() + ".provider", file)
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri)
startActivityForResult(intent, REQUEST_CODE)
}
private fun captureVideo() {
val file = File(requireContext().filesDir.path, "test.mp4")
val uri = FileProvider.getUriForFile(requireActivity(), BuildConfig.APPLICATION_ID.toString() + ".provider", file)
val intent = Intent(MediaStore.ACTION_VIDEO_CAPTURE)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri)
startActivityForResult(intent, REQUEST_VIDEO)
}
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(requireActivity(), BuildConfig.APPLICATION_ID.toString() + ".provider", file)
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri)
startActivityForResult(intent, REQUEST_CODE)
}
奖励:我建议您看一下 MediaStore class if you're planning on making a media-oriented app. Here's an official intro for it.
网上有很多关于这个话题的问答,但不幸的是没有解决我的问题。
每次我尝试将相机用于我的 Fragment 中的视频录制图片时,我都会收到错误消息。 我要保存这些文件的路径在 /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.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.access00(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>
我正在使用以下函数调用相机:
private fun capturePhoto() {
val file = File(requireContext().filesDir.path, "test.jpg")
val uri = FileProvider.getUriForFile(requireActivity(), BuildConfig.APPLICATION_ID.toString() + ".provider", file)
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri)
startActivityForResult(intent, REQUEST_CODE)
}
private fun captureVideo() {
val file = File(requireContext().filesDir.path, "test.mp4")
val uri = FileProvider.getUriForFile(requireActivity(), BuildConfig.APPLICATION_ID.toString() + ".provider", file)
val intent = Intent(MediaStore.ACTION_VIDEO_CAPTURE)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri)
startActivityForResult(intent, REQUEST_VIDEO)
}
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(requireActivity(), BuildConfig.APPLICATION_ID.toString() + ".provider", file)
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri)
startActivityForResult(intent, REQUEST_CODE)
}
奖励:我建议您看一下 MediaStore class if you're planning on making a media-oriented app. Here's an official intro for it.