showing results for - "how to download an mp3 file in react native"
Emma
27 Apr 2017
1// use this package rn-fetch-blob
2requestToPermissions = async () => {
3    try {
4      const granted = await PermissionsAndroid.request(
5        PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
6        {
7          title: 'Music',
8          message:
9            'App needs access to your Files... ',
10          buttonNeutral: 'Ask Me Later',
11          buttonNegative: 'Cancel',
12          buttonPositive: 'OK',
13        },
14      );
15      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
16        console.log('startDownload...');
17        this.startDownload();
18      }
19    } catch (err) {
20      console.log(err);
21    }
22  };
23
24
25startDownload = () => {
26    const {tunes, token, currentTrackIndex} = this.state;
27    let {url, name} = tunes[currentTrackIndex];
28    RNFetchBlob.config({
29      fileCache: true,
30      appendExt: 'mp3',
31      addAndroidDownloads: {
32        useDownloadManager: true,
33        notification: true,
34        title: name,
35        path: RNFetchBlob.fs.dirs.DownloadDir + `${name}`, // Android platform
36        description: 'Downloading the file',
37      },
38    })
39      .fetch('GET', url)
40      .then(res => {
41        console.log('res', res);
42        console.log('The file is save to ', res.path());
43      });
44  };
45