使用RNFetchBlob

问题描述

我有以下代码,用于从通过我的应用程序使用相同路径创建的下载文件夹中取消链接/删除文件

注意,我正在使用RNFetchBlob软件包。

---
const fs = RNFetchBlob.fs
const base64 = RNFetchBlob.base64
const dirs = RNFetchBlob.fs.dirs

RNFetchBlob.fs
      .unlink(dirs.DownloadDir + '/passpoint.config.xml')
      .then(() => {
        alert("File deleted");
      })
      .catch(err => {
        alert(err);
      });
---

我不断收到以下错误

[Error: Failed to delete '/storage/emulated/0/Download/passpoint.config.xml']

我以为可能是路径,但这与我创建文件时使用的路径相同,我可以通过Android上的文件资源管理器查看文件

解决方

fs.unlink(dirs.DownloadDir + '/passpoint.config.xml');

解决方法

只需在 AndroidManifyingt.xml 文件中添加 android:requestLegacyExternalStorage="true"

<application
android:name=".MainApplication"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:allowBackup="false"
android:theme="@style/AppTheme"
android:requestLegacyExternalStorage="true">
,

使用 MANAGE_EXTERNAL_STORAGE 权限作为快速解决方案(编辑 AndroidManifiest.xml

https://developer.android.com/training/data-storage/manage-all-files

或者使用更高级的技术:

https://developer.android.com/training/data-storage/shared/media

https://developer.android.com/training/data-storage/shared/documents-files