如何配置FileProvider的file_paths.xml匹配指定下载路径?
Correct FileProvider Setup for Your Private External Download Directory
Got it, let's fix this FileProvider path issue once and for all. Your APK is stored in the private external files directory of your app specifically under the Download subfolder, so we need to use the right path tag in file_paths.xml that maps directly to this location.
Step 1: The Correct file_paths.xml Configuration
Replace your existing file content with this:
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-files-path name="app_downloads" path="Download/" /> </paths>
Why This Works (And Why Your Previous Attempts Failed)
Let's break down the details:
external-files-path: This tag is designed explicitly for mapping the directory returned byContext.getExternalFilesDir(null)— which is exactly the root path you're using withSetDestinationInExternalFilesDir(/storage/emulated/0/Android/data/com.myco.myapp/files/).name="app_downloads": This is just a friendly placeholder that will appear in the generated Content URI (e.g.,content://com.myco.myapp.fileprovider/app_downloads/com.myco.myapp.version.apk). You can rename this to anything meaningful for your app.path="Download/": This points to theDownloadsubfolder relative to the root of your app's external files directory. No leading slash needed here since it's a relative path.
Your earlier issues happened because:
- Using tags like
external-pathtargets the public external storage root (/storage/emulated/0/), which doesn't align with your app's private directory — hence theIllegalArgumentException. - The
root-pathhack matches all paths but produces malformed URIs (with thefile:/prefix baked in) that the system can't properly resolve for installation.
What the Generated URI Will Look Like
With this configuration, GetUriForFile will output a valid Content URI like:content://com.myco.myapp.fileprovider/app_downloads/com.myco.myapp.version.apk
This URI is fully compatible with Android's file access rules and will work for app installation.
Quick Checks to Avoid Issues
- Make sure your
apkPathvariable is a plain file path (nofile:/prefix) — it should be/storage/emulated/0/Android/data/com.myco.myapp/files/Download/com.myco.myapp.version.apk. - If you're using AndroidX instead of the old support library, just update the provider name in your Manifest to
androidx.core.content.FileProvider— thefile_paths.xmlconfiguration stays identical.
内容的提问来源于stack exchange,提问作者Brian T




