Android应用执行时崩溃求助(涉及provider_paths.xml与Manifest)
Hey there, let's work through your Android app crash issues related to provider_paths.xml and the AndroidManifest.xml—these are super common pitfalls, so I’ll walk you through the most likely fixes step by step:
This is where most crashes start. Make sure your provider entry has all the required attributes:
- Never set
android:exported="true": FileProvider is designed to be private, so setting this totruewill immediately throw a security exception. It must beandroid:exported="false". - Include
android:grantUriPermissions="true": This lets your app grant temporary access to files via URIs, which is mandatory for FileProvider to work. - Use a unique authority: Stick to
${applicationId}.fileproviderto avoid conflicts with other apps. The authority must match exactly what you use in your code later.
Here's a correct example of the declaration:
<provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" /> </provider>
provider_paths.xml File A misformatted or incorrect paths file is another top culprit:
- Location matters: The file must live in
res/xml/(create thexmldirectory if it doesn't exist). If it's in the wrong place, the app will fail to find the resource and crash. - Root tag is
<paths>: Don't accidentally use<path>(singular) as the root—this will cause a parsing error. - Use valid path tags: Only use the predefined tags like
<files-path>,<external-files-path>,<external-path>, etc. Each tag maps to a specific storage location. For example, to access images in your app's external storage directory:
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Maps to Context.getExternalFilesDir(Environment.DIRECTORY_PICTURES) --> <external-files-path name="app_images" path="Pictures/" /> </paths>
- Avoid absolute paths: FileProvider doesn't allow direct absolute paths—stick to the tag-based structure to ensure security and compatibility.
Even if your config files are correct, a mismatch in how you generate URIs will cause crashes:
- Always use
FileProvider.getUriForFile()to create URIs, never build them manually. - Ensure the authority string you pass to this method matches exactly what's in your AndroidManifest.xml. For example:
// Correct way to generate a URI File targetFile = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "profile.jpg"); Uri fileUri = FileProvider.getUriForFile( this, getPackageName() + ".fileprovider", // Matches the authority in Manifest targetFile );
If your app accesses external storage, you need to request the appropriate dynamic permissions:
- For reading files, request
READ_EXTERNAL_STORAGE(orREAD_MEDIA_IMAGES/READ_MEDIA_VIDEOfor Android 13+). - For writing files, request
WRITE_EXTERNAL_STORAGE(though this is deprecated in newer Android versions—use scoped storage instead if possible). - A missing permission will lead to a
SecurityExceptionwhen trying to access files, which crashes the app.
If the above steps don't fix the crash, look at the full stack trace in Logcat—this will tell you exactly what's wrong:
- If you see
IllegalArgumentException: Failed to find configured root that contains..., yourprovider_paths.xmldoesn't include the path to the file you're trying to access. - If you see
SecurityException: Provider must not be exported, you forgot to setandroid:exported="false"in the Manifest. - If you see
Resources$NotFoundException: Resource ID #0x7f080001 type #0x12 is not valid, yourprovider_paths.xmlis missing or in the wrong directory.
内容的提问来源于stack exchange,提问作者Kiranjit

