1

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?
Gillardo
  • 9,518
  • 18
  • 73
  • 141
  • I don't think the application GUID has to change. It was happening on my device because there was another version of the app installed, if I remember correctly. After I deleted it, the GUID stopped changing. I don't know if you can load images by setting the source to a CDVFile://localhost/ path, but if so, then you could get a local path from the FileSystem and just save that. – kindasimple Nov 08 '14 at 06:23
  • [It looks like you can](http://stackoverflow.com/questions/23784480/phonegap-display-images-with-the-path-cdvfile-localhost-persistent). – kindasimple Nov 08 '14 at 06:25

1 Answers1

1

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

kindasimple
  • 2,427
  • 1
  • 16
  • 20
  • The cdvfile:// url doesn't seem to work if you want to use it as a src for audio or video tags for playing media. Only seems to work for images. – Playforward Jun 19 '15 at 19:10
  • @kindasimple i just store my image fileSystem.root.getDirectory/SmartMEdia/Jellyfish.jpg.. can you please tell me how i can access this file ? – Rahul Sharma Sep 07 '16 at 11:34
  • @DustinDempsey If using angular, you may need to do: var app = angular.module( 'myApp', [] ) .config( [ '$compileProvider', function( $compileProvider ) { $compileProvider.imgSrcSanitizationWhitelist(/^\s*(https?|file|blob|cdvfile):|data:image\//); } ]); courtesy: https://forum.ionicframework.com/t/displaying-image-file-from-android-filesystem/1994/5 – Madhup Singh Yadav Apr 16 '17 at 08:23