簡體   English   中英

下載圖像在 IOS 設備上不起作用 - React Native

[英]Download image not working on IOS device - React Native

我使用RNFetchBlobReact Native 中實現了下載圖像的功能,它在 android 上工作正常,但在 IOS 設備上它不起作用。

以下是我用於下載功能的本機代碼。

downloadImg = (url) => {
      var that = this;
      async function requestCameraPermission() {
        const granted = await PermissionsAndroid.request(
          PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
          {
            title: 'Test App',
            message: 'Test App needs access to your gallery ',
          }
        );
        if (granted === PermissionsAndroid.RESULTS.GRANTED) {
          that.actualDownload(url);
        } else {
          Alert.alert('Permission Denied!', 'You need to give storage permission to download the file');
        }
      }
      if (Platform.OS === 'android') { 
        requestCameraPermission();
      } else {
        this.actualDownload(url);
      }
}


actualDownload = (url) => {
          var date = new Date();
          var image_URL = url;
          var ext = this.getExtention(image_URL);
          ext = "." + ext[0];
          const { config, fs } = RNFetchBlob;
          let PictureDir = Platform.OS === 'ios' ? fs.dirs.DocumentDir : fs.dirs.PictureDir;
          let options = {
          fileCache: true,
          addAndroidDownloads: {
              useDownloadManager: true,
              notification: true,  
              path: PictureDir + "/Images/image_" + Math.floor(date.getTime()
              + date.getSeconds() / 2) + ext,
              description: 'Image'
          }
          }
          config(options).fetch('GET', image_URL).then((res) => {
              console.log("Thank you","Image Downloaded Successfully."); 
          });
    }

addAndroidDownloads 僅適用於 android。 當您使用 addAndroidDownloads 時,配置中的路徑並非無用。 但是對於ios,必須添加路徑。 試試下面的代碼,你可以為ios添加進度。

actualDownload = (url) => {
          var date = new Date();
          var image_URL = url;
          var ext = this.getExtention(image_URL);
          ext = "." + ext[0];
          const { config, fs } = RNFetchBlob;
          let PictureDir = Platform.OS === 'ios' ? fs.dirs.DocumentDir : fs.dirs.PictureDir;
          let options = {
          fileCache: true,
          path: PictureDir + "/Images/image_" + Math.floor(date.getTime() // add it
          addAndroidDownloads: {
              useDownloadManager: true,
              notification: true,  
              path: PictureDir + "/Images/image_" + Math.floor(date.getTime()
              + date.getSeconds() / 2) + ext,
              description: 'Image'
          }
          }
          config(options).fetch('GET', image_URL).then((res) => {
              console.log("Thank you","Image Downloaded Successfully."); 
          });
    }

這是文件說:

使用 DownloadManager 時,config 中的 fileCache 和 path 屬性不會生效,因為 Android DownloadManager 只能將文件存儲到外部存儲,還要注意 Download Manager 只能支持 GET 方法,這意味着請求正文將被忽略。

該方法也適用於 ios。 通知沒有來。 如果你想檢查它是否被下載,你可以通過在終端(Mac)或cmd(Windows)中記錄路徑並通過終端本身訪問它來檢查它。 它可能在 finder (Mac) 或文件資源管理器 (Windows) 中不可見。

您可以通過這種方式從函數中記錄的路徑: cd ~/Library/Developer/CoreSimulator/Devices...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM