You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

1. Double-Check Your FileProvider Declaration in AndroidManifest.xml

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 to true will immediately throw a security exception. It must be android: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}.fileprovider to 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>
2. Validate the provider_paths.xml File

A misformatted or incorrect paths file is another top culprit:

  • Location matters: The file must live in res/xml/ (create the xml directory 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.
3. Check Your URI Generation Code

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
);
4. Verify Runtime Permissions (Android 6.0+)

If your app accesses external storage, you need to request the appropriate dynamic permissions:

  • For reading files, request READ_EXTERNAL_STORAGE (or READ_MEDIA_IMAGES/READ_MEDIA_VIDEO for 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 SecurityException when trying to access files, which crashes the app.
5. Use Logcat to Pinpoint the Exact Issue

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..., your provider_paths.xml doesn't include the path to the file you're trying to access.
  • If you see SecurityException: Provider must not be exported, you forgot to set android:exported="false" in the Manifest.
  • If you see Resources$NotFoundException: Resource ID #0x7f080001 type #0x12 is not valid, your provider_paths.xml is missing or in the wrong directory.

内容的提问来源于stack exchange,提问作者Kiranjit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:13:39