Gillardo
Gillardo

Reputation: 9828

Load images saved in iOS App Documents directory from cordova html page (cordova)?

I have built 1 app that has to run on iOS, android and Windows Phone. all work fine apart from iOS. My app takes a photo using the camera, resizes that image and saves the image.

On iOS, these images were being saved into the tmp directory of the application, which was being deleted, so now i save the images into the Documents folder.

I then save the path to this image into my sqlite database. On my html page, i reference the file using the url, such as

var/mobile/application/GUID-HERE/Documents/imageName.jpg

Now it seems that when i rebuild my application in xCode, the application guid is changed, so all my file references that have been previously saved, are now invalid.

So

  1. Can i reference the documents folder relatively from my HTML page?

OR

  1. Can i stop my application changing this GUID?

Upvotes: 1

Views: 4163

Answers (1)

kindasimple
kindasimple

Reputation: 2427

Use the toInternalURL() property of the File plugin. After you save your image, you can get a local url without that GUID and save that to your database instead. The url has this format

cdvfile://localhost/persistent/path/to/file

Documentation

An example: (mostly from file-transfer docs. requires file and file-transfer plugins)

resolveLocalFileSystemURL(cordova.file.documentsDirectory, function success(entry) {
    download(entry.toInternalURL() + "image.jpg");
});

var download = function(localUrl) {

    var fileTransfer = new FileTransfer();
    var uri = encodeURI("https://cordova.apache.org/images/cordova_bot.png");

    fileTransfer.download(
      uri,
      localUrl,
      function(entry) {
          document.body.innerHTML = entry.toInternalURL();
          var img = document.createElement("img");
          img.setAttribute('src', entry.toInternalURL());
          document.body.appendChild(img);

      },
      function(error) {
          console.log("error code" + error.code);
      },
      false,
      {
          headers: {
              "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
          }
      }
  );
}

img

Upvotes: 1

Related Questions