如何将图像从服务器保存到离子应用程序的设备内部存储器中?

How to save images from server into device internal memory in ionic app?

我尝试了大部分与 cordova 文件相关的插件,最终得出了这段代码。它给出了成功信息,但它们没有出现在画廊中。有人可以帮忙吗?

    $scope.download = function download(name, contentType, fileLInk) {
ionic.Platform.ready(function() {
  var url = apiServiceBaseUri + fileLInk;
  var targetPath = cordova.file.dataDirectory + 'myapp/' + name;
  var trustHosts = false;
  var options = {
    headers: {
      'Authorization': tokenType + ' ' + accessToken
    }
  };

  $cordovaFileTransfer.download(url, targetPath, options, trustHosts)
    .then(function(result) {
      var alertPopup = $ionicPopup.alert({
        title: 'File has been downloaded',
      });
      $ionicListDelegate.closeOptionButtons();
    }, function(error) {
      $ionicListDelegate.closeOptionButtons();
      if (error.http_status == 401) {
        $ionicPopup.alert({
          title: 'Oops, Session is expired!',
          template: 'Looks like your session is expired or you logged in from someother device.'
        });
        $ionicHistory.clearCache().then(function() {
          $state.go('start.login');
        });
      }
      var alertPopup = $ionicPopup.alert({
        title: 'Sorry, something went wrong during the request!',
        template: error.data.errors[0].message
      });
    });
});
};

你应该在文件传输后使用这个插件

cordova plugin add cordova-plugin-refresh-gallery

并在文件传输成功回调中使用此函数

$cordovaFileTransfer.download(url, targetPath, options, trustHosts)
    .then(function(result) {
     refreshMedia.refresh(targetPath); 
     //some other things 

我的工作代码

 $cordovaFileTransfer.download(url, targetPath, {}, true).then(function (result) {
               refreshMedia.refresh(targetPath);
               $scope.fullViewImageDownloadSpiner = false;
               tostService.notify('Image Downloaded', 'top');
           }, function (error) {
               $scope.fullViewImageDownloadSpiner = false;
               tostService.notify('Try Again', 'top');
           }, function (progress) {
               // PROGRESS HANDLING GOES HERE
               //  $timeout(function () {
               //     $scope.downloadProgress = (progress.loaded / progress.total) * 100;
               // })
           });

并将目标路径设置为

var filename = url.split("/").pop();
var targetPath = cordova.file.externalRootDirectory + '/Download/' + filename;

此插件更新图像 gallery.When 您将图像保存在 android 设备上,但图像未显示在图库中。此插件可以工作

希望这能解决您的问题.. 谢谢 :)