Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 下载本地音频文件?_Javascript_Android_React Native_Rn Fetch Blob - Fatal编程技术网

Javascript 下载本地音频文件?

Javascript 下载本地音频文件?,javascript,android,react-native,rn-fetch-blob,Javascript,Android,React Native,Rn Fetch Blob,我正在尝试使用rn fetch blob下载音频文件mp3 因此,我有不止一个案例: 当我尝试像这样向RNFetchBlob配置添加路径时 const pathFile = RNFetchBlob.fs.dirs.MainBundleDir // Or .DocumentDir // Others is crashed my App 'Android'; 然后回调成功,因此它是日志 该文件将保存到 /data/user/0/com.music/files/RNFetchBlobTmp_1eoi

我正在尝试使用rn fetch blob下载音频文件mp3

因此,我有不止一个案例:

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

const pathFile = RNFetchBlob.fs.dirs.MainBundleDir // Or .DocumentDir // Others is crashed my App 'Android';
然后回调成功,因此它是日志

该文件将保存到 /data/user/0/com.music/files/RNFetchBlobTmp_1eoigyb8hoscuizsue.mp3

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

所以在安卓系统中也有这样一个问题

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));
  };

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

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

所以

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());
      });
  };