我需要使用 React Native 应用程序将文件下载到用户的下载目录rn 获取 blob https://www.npmjs.com/package/rn-fetch-blob,但似乎与 Android 10 不兼容,因为我收到错误:
首先我请求权限:
try {
const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
await actualDownloadPDF();
} else {
ToastAndroid.show('Nepovolili jste ukládání souborů do zařízení.', ToastAndroid.LONG);
}
} catch (err) {
console.warn(err);
}
然后我尝试下载文件:
const res = await RNFetchBlob.config({
addAndroidDownloads: {
useDownloadManager: true,
notification: true,
mime: 'application/pdf',
title: filename,
path: dirs.DownloadDir,
},
}).fetch('GET', url, {
Authorization: 'Bearer ' + Api.Bearer,
});
似乎 Android 10 要求开发人员打开 Intent,其中用户选择对文件夹的写入权限(https://developer.android.com/training/data-storage/shared/documents-files#grant-access-directory https://developer.android.com/training/data-storage/shared/documents-files#grant-access-directory),但我不知道该怎么做,因为 RNFetchBlob 似乎还没有这个功能。
有什么想法可以让这项工作发挥作用吗?它在 Android 9 上运行良好。