📅  最后修改于: 2022-03-11 15:04:25.353000             🧑  作者: Mango
// use this package rn-fetch-blob
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());
});
};