美文网首页
RN 下载图片、视频保存相册

RN 下载图片、视频保存相册

作者: 精神病患者link常 | 来源:发表于2021-09-13 10:12 被阅读0次
    import { PermissionsAndroid, Platform } from "react-native";
    import CameraRoll from "@react-native-community/cameraroll";
    import RNFS from 'react-native-fs'
    
    async function hasAndroidPermission() {
      const permission = PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE;
    
      const hasPermission = await PermissionsAndroid.check(permission);
      if (hasPermission) {
        return true;
      }
    
      const status = await PermissionsAndroid.request(permission);
      return status === 'granted';
    }
    
    export const saveImageToPhoto = async(imageUrl)=>{
      downLoad(imageUrl,'png')
    }
    export const saveVideoToPhoto = async (videouRL)=>{
      downLoad(videouRL,'mp4')
    }
    const downLoad = async (url,type)=>{
      if (Platform.OS === "android" && !(await hasAndroidPermission())) {
        // 提示
        return;
      }
      let timestamp = (new Date()).getTime();
      let random = String(((Math.random() * 1000000) | 0))
      let dirs = Platform.OS === 'ios' ? RNFS.DocumentDirectoryPath : 'file://' + RNFS.ExternalStorageDirectoryPath;
      const toPath = `${dirs}/${timestamp+random}.${type}`;
      console.log('url==',url);
      console.log('toPath==',toPath);
      const options = {
        fromUrl: url,
        toFile: toPath,
        background: true,
        begin: (res) => {
        },
        progress: (res) => {
        }
      };
      try {
        const ret = RNFS.downloadFile(options);
        ret.promise.then(res => {
          const filePath = toPath.startsWith('file://') ? toPath : 'file://' + toPath 
          console.log('filePath===',filePath);
          CameraRoll.save(filePath).then(function(result) {
            console.log('saveToCameraRoll success');
            RNFS.unlink(toPath)
          }).catch(function(error) {
              console.log(' saveToCameraRollerror', error);
          });
        }).catch(err => {
        });
      } catch (e) {
      }
    }
    
    

    相关文章

      网友评论

          本文标题:RN 下载图片、视频保存相册

          本文链接:https://www.haomeiwen.com/subject/dpoxgltx.html