在 AndroidManifest.xml 文件中,需要在 标签下添加以下内容:

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.fileprovider"
    android:grantUriPermissions="true"
    android:exported="false">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

其中,${applicationId} 是你的应用程序包名,file_paths.xml 是文件路径的配置文件,在 res/xml/ 目录下,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="download" path="Download/"/>
</paths>

这里的配置是将下载文件保存在外部存储的 Download 目录下,其他目录也可以根据需要进行配置。在代码中获取下载文件的 Uri 时,需要使用以下代码:

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "filename");
Uri uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileprovider", file);

其中,"filename" 是要下载的文件名,context 是当前的上下文。在 Android 7.0 及以上版本中,要使用 FileProvider 来获取文件 Uri,否则会抛出 FileUriExposedException 异常

蒲公英自动下载时androidauthorities怎么配置

原文地址: https://www.cveoy.top/t/topic/cFsU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录