无法在离子中显示已保存的图像

时间:2017-01-17 16:59:59

标签: cordova ionic-framework cordova-plugins ngcordova

在我的应用程序中,我创建了一个图库,用于显示图像,还可以在指定路径中下载图像。

唯一的问题是,我无法将下载的图像显示到设备库。

我使用过MediaScanner插件但没有使用。我已经好几天了。

任何帮助都将受到高度赞赏。

//	Downloading Function
this.download = function(url, path) {
  ionic.Platform.ready(function() {
    var platform = $cordovaDevice.getPlatform();
    alert(platform);
    var filename = url.split("/").pop();
    var directory = "Eshiksa";
    $cordovaFile.createDir(directory, false);
    var targetPath = cordova.file.externalRootDirectory + directory + "/" + path + "/" + filename;
    var trustHosts = true;
    var options = {};
    alert("Downloading Starting");

    if (platform == "Android") {
      cordova.plugins.diagnostic.getPermissionAuthorizationStatus(function(status) {
        switch (status) {
          case cordova.plugins.diagnostic.permissionStatus.GRANTED:
            $cordovaFileTransfer.download(url, targetPath, options, trustHosts)
              .then(function(result) {
                  var save_location = 'Save file on ' + targetPath + ' success!';

                  alert(save_location);

                  if (path == "Invoice") {
                    ionic.Platform.ready(function() {
                      $cordovaFileOpener2.open(
                        targetPath, 'application/pdf'
                      ).
                      then(function() {
                          console.log('Success');
                        },
                        function(err) {
                          alert("Unable to open file, it is downloaded at download folder");
                          console.log('An error occurred: ' + JSON.stringify(err));
                        });
                    });
                  }
                },
                function(error) {
                  alert(JSON.stringify(error));

                  save_location = 'Error Download file';
                  alert(save_location);
                },
                function(progress) {
                  this.downloadProgress = parseInt((progress.loaded / progress.total) * 100);

                });
            break;
          default:
            cordova.plugins.diagnostic.requestRuntimePermission(function(status) {
              switch (status) {
                case cordova.plugins.diagnostic.permissionStatus.GRANTED:
                  $cordovaFileTransfer.download(url, targetPath, options, trustHosts)
                    .then(function(result) {
                        var save_location = 'Save file on ' + targetPath + ' success!';

                        alert(save_location);

                        if (path == "Invoice") {
                          ionic.Platform.ready(function() {
                            $cordovaFileOpener2.open(
                              targetPath, 'application/pdf'
                            ).
                            then(function() {
                                console.log('Success');
                              },
                              function(err) {
                                alert("Unable to open file, it is downloaded at download folder");
                                console.log('An error occurred: ' + JSON.stringify(err));
                              });
                          });
                        }
                      },
                      function(error) {
                        alert(JSON.stringify(error));

                        save_location = 'Error Download file';
                        alert(save_location);
                      },
                      function(progress) {
                        this.downloadProgress = parseInt((progress.loaded / progress.total) * 100);

                      });
                  break;
                default:
                  alert("Unable to download. You may have not granted us the permission for writing file on device");
                  break;
              }
            }, function(error) {
              console.error("The following error occurred: " + error);
            }, cordova.plugins.diagnostic.permission.WRITE_EXTERNAL_STORAGE);
            break;
        }
      }, function(error) {
        console.error("The following error occurred: " + error);
      }, cordova.plugins.diagnostic.permission.WRITE_EXTERNAL_STORAGE);

    } else {
      $cordovaFileTransfer.download(url, targetPath, options, trustHosts)
        .then(function(result) {
            var save_location = 'Save file on ' + targetPath + ' success!';

            alert(save_location);

            if (path == "Invoice") {
              ionic.Platform.ready(function() {
                $cordovaFileOpener2.open(
                  targetPath, 'application/pdf'
                ).
                then(function() {
                    console.log('Success');
                  },
                  function(err) {
                    alert("Unable to open file, it is downloaded at download folder");
                    console.log('An error occurred: ' + JSON.stringify(err));
                  });
              });
            }
          },
          function(error) {
            alert(JSON.stringify(error));

            save_location = 'Error Download file';
            alert(save_location);
          },
          function(progress) {
            this.downloadProgress = parseInt((progress.loaded / progress.total) * 100);

          });
    }

  });
};

0 个答案:

没有答案