1

我正在尝试使用rn-fetch-blob.

所以我有不止一个案例:)

当我尝试像这样添加 RNFetchBlob 配置的路径时

const pathFile = RNFetchBlob.fs.dirs.MainBundleDir // Or .DocumentDir // Others is crashed my App 'Android';

回调“then”成功,所以它是 Log

The file is saved to /data/user/0/com.music/files/RNFetchBlobTmp_1ueoigyb8hoscuuizzsue.mp3

但是当我尝试在我的真实设备中探索这个文件时,我找不到它们,我不知道为什么!

那么在Android中是否存在类似的问题?

startDownload = () => {
    const {url} = this.state;
    const pathFile = RNFetchBlob.fs.dirs.DocumentDir;
    let date = new Date();
    RNFetchBlob.config({
      fileCache: true,
      path:
        pathFile +
        '/me_' +
        Math.floor(date.getTime() + date.getSeconds() / 2),
    })
      .fetch('GET', url)
      .then(res => {
        console.log('The file is save to ', res.path()); // It's log here but i can't see the file :\
      })
      .catch(err => console.log('err', err));
  };
4

1 回答 1

2

我首先通过请求权限来解决这个问题,然后调用下载功能,

但实际上我不知道他们为什么在请求许可之前告诉我首先下载的文件:\

所以

requestToPermissions = async () => {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
        {
          title: 'Music',
          message:
            'App needs access to your Files... ',
          buttonNeutral: 'Ask Me Later',
          buttonNegative: 'Cancel',
          buttonPositive: 'OK',
        },
      );
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        console.log('startDownload...');
        this.startDownload();
      }
    } catch (err) {
      console.log(err);
    }
  };


startDownload = () => {
    const {tunes, token, currentTrackIndex} = this.state;
    let {url, name} = tunes[currentTrackIndex];
    RNFetchBlob.config({
      fileCache: true,
      appendExt: 'mp3',
      addAndroidDownloads: {
        useDownloadManager: true,
        notification: true,
        title: name,
        path: RNFetchBlob.fs.dirs.DownloadDir + `${name}`, // Android platform
        description: 'Downloading the file',
      },
    })
      .fetch('GET', url)
      .then(res => {
        console.log('res', res);
        console.log('The file is save to ', res.path());
      });
  };
于 2020-02-28T23:21:50.377 回答