I'm trying to download audio file "mp3" using rn-fetch-blob
.
So I have more than one cases :)
When I trying to add a path to RNFetchBlob config like this
const pathFile = RNFetchBlob.fs.dirs.MainBundleDir // Or .DocumentDir // Others is crashed my App 'Android';
The callback "then" get success so it's Log
The file is saved to /data/user/0/com.music/files/RNFetchBlobTmp_1ueoigyb8hoscuuizzsue.mp3
But when I try to explore this file in my real device I can't find them I don't know why!
So there's one faced same issue like this in 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));
};
I solve this issue by request for permission firstly then call the download function,
But actually I don't know why they tell me the file downloaded firstly before asking for permission :\
So
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());
});
};