如何在React Native中下载文件(图像,GIF等)

时间:2018-10-23 07:17:01

标签: react-native react-native-android

  

我已经尝试过这段代码,但是我的应用程序崩溃了却没有显示任何错误,我无法弄清楚是什么错误

const {config,fs} = RNFetchBlob;
const { params } = this.props.navigation.state;
let PictureDir = fs.dirs.PictureDir // this is the pictures directory. You can check the available directories in the wiki.
let options = {
fileCache: true,
addAndroidDownloads : {
    useDownloadManager : false, // setting it to true will use the device's native download manager and will be shown in the notification bar.
    notification : false,
    path:  PictureDir + "/me_image", // this is the path where your downloaded file will live in
    }
}
config(options).fetch('GET',url).then((res) => {
    alert('Done');
})

1 个答案:

答案 0 :(得分:0)

  saveToCameraRoll = () => {
    const{selectedImage, downlaodUrl} = this.state
    let url = downlaodUrl[0].url;
    ToastAndroid.show("Image is Saving...", ToastAndroid.SHORT)
    if (Platform.OS === 'android'){ 

      RNFetchBlob
        .config({
          fileCache : true,
          appendExt : 'jpg'
        })
        .fetch('GET', url)
        .then((res) => {
            console.log()
         CameraRoll.saveToCameraRoll(res.path())

            .then((res) => {
             console.log("save", res)
             ToastAndroid.show("Image saved Successfully.", ToastAndroid.SHORT)
            }).catch((error) => {
                ToastAndroid.show("Ops! Operation Failed", ToastAndroid.SHORT)

            })

        })
    } else {
        CameraRoll.saveToCameraRoll(url)
          .then(alert('Success', 'Photo added to camera roll!'))
          ToastAndroid.show("Image saved Successfully.", ToastAndroid.SHORT)
    }
  }