Phonegap iOS file.path

I have an application using cordova 3.4 and file 1.1.0. If I copy an image using the camera module, I use

myFileObj.path = file.toNativeURL()

to get the file path. If I put this path in the img tag, I will be shown an image on Android. On iOS, this does not work. Result of file.toNativeURL ():

myFileObj.path -> file:///Users/.../Library/Application%20Support/..../myFolder/myImage.JPG

Using file 1.0, I had to create a url and it looked like this:

myFileObj.path = dirTarget.toURL() + '/' + targetFileName 
myFileObj.path -> cdvfile://localhost/persisten/myFolder/myImage.JPG

If the video and audio did not work, but at least the photos.

Using the file 1.1.0 / 1.1.1, the result of this method is also different:

myFileObj.path -> file:///Users/mak/Library/.../myFolder/myImage.JPG?id=.....&ext=JPG

This also does not work on iOS.

How can I get a working file path using the cordova file module versions 1.1.0 and 1.1.1?

EDIT: what am I doing and what is not working:

, . Android iOS: Android src media-tags , iOS src.

-:

navigator.camera.getPicture(onSuccess, onFail, {
      destinationType: Camera.DestinationType.NATIVE_URI,
      sourceType : Camera.PictureSourceType.PHOTOLIBRARY,
      mediaType: Camera.MediaType.ALLMEDIA
  });

:

function onSuccess(imageData) {
    A.StoreFile(imageData, id);
}

:

A.StoreFile = function(file, idBox) {

    var targetDirectory = Config.getRootPath();
    window.resolveLocalFileSystemURL(file, resolveFileSystemSuccess, resolveFileSystemError);


    function resolveFileSystemSuccess(fileEntry) {
    fileEntry.file(function(filee) {
        mimeType = filee.type;
        getFileSuccess(fileEntry, mimeType);
    }, function() {
    });
}


function getFileSuccess(fileEntry, mimeType) {

    var targetFileName = name + '.' + fileNativeType;

    var parentName = targetDirectory.substring(targetDirectory.lastIndexOf('/')+1),
    parentEntry = new DirectoryEntry(parentName, targetDirectory);


    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
        fileSystem.root.getDirectory(targetDirectory, {create: true, exclusive: false}, function(dirTarget) {
            fileEntry.copyTo(dirTarget, targetFileName, function(entry) {
                addFileToLocalStorage(entry);
            }, function() {
            })
        })
    }, resolveFileSystemError);

}

- localStorageObject

function addFileToLocalStorage(file) {
    fileList.addFile(
        {
            name:file.name,
            internalURL: file.toNativeURL()
        });
}

dom:

myElement.find('.myMimeTypeTag').attr('src', fileList[f].internalURL);

android, iOS.

iOS- img-:

enter image description here

:

DEPRECATED: Update your code to use 'toURL'

toURL

id="org.apache.cordova.file"
version="1.1.1-dev"
+4
2

, - . copy . , Android , ios - . , .

0

( , , , , , , , . .)

Cordova 3.5.0, , File 1.1.0 Camera 0.2.9.

, cordova

cordova create so23801369 com.example.so23801369 so23801369
cd so23801369
cordova platform add ios
cordova plugin add org.apache.cordova.file
cordova plugin add org.apache.cordova.camera

"Hello, Cordova" , , ( ) , .

index.html:

<button id="doit">Do it</button>
<img class="myMimeTypeTag" src="file:///nothing" />

www/js/index.js, :

var app = {

    initialize: function() {
        // Don't activate the button until Cordova is initialized
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },

    onDeviceReady: function() {
        document.getElementById('doit').addEventListener('click', app.runTest, false);
    },

    runTest: function(ev) {
        var StoreFile = function(file) {
            var targetDirectory = "myFolder";
            window.resolveLocalFileSystemURL(file, resolveFileSystemSuccess, resolveFileSystemError);

            function resolveFileSystemSuccess(fileEntry) {
                console.log("resolveLocalFileSystemURL returned: ", fileEntry.toURL());
                fileEntry.file(function(filee) {
                    mimeType = filee.type;
                    getFileSuccess(fileEntry, mimeType);
                }, function() {
                });
            }
            function resolveFileSystemError() {
                console.log("resolveFileSystemError: FAIL");
                console.log(arguments);
                alert("FAIL");
            }

            function getFileSuccess(fileEntry, mimeType) {
                var targetFileName = "myImage.JPG";
                window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
                    fileSystem.root.getDirectory(targetDirectory, {create: true, exclusive: false}, function(dirTarget) {
                        fileEntry.copyTo(dirTarget, targetFileName, function(entry) {
                            console.log("copyTo returned: ", entry.toURL());
                            // I have replaced the localstorage handling with this code
                            // addFileToLocalStorage(entry);
                            var img = document.querySelector('.myMimeTypeTag');
                            img.setAttribute('src', entry.toNativeURL());
                        }, function() {
                        });
                    });
                }, resolveFileSystemError);
            }
        };

        var onSuccess = function(imageData) {
            console.log("getPicture returned: ", imageData);
            StoreFile(imageData);
        };

        var onFail = function() {
            console.log("getPicture FAIL");
            console.log(arguments);
            alert("FAIL");
        };

        ev.preventDefault();
        ev.stopPropagation();
        navigator.camera.getPicture(onSuccess, onFail, {
            destinationType: Camera.DestinationType.NATIVE_URI,
            sourceType : Camera.PictureSourceType.PHOTOLIBRARY,
            mediaType: Camera.MediaType.ALLMEDIA
        });
    }
};

, -, , src URL- . Safari dev iPad, :

[Log] getPicture returned:  assets-library://asset/asset.JPG?id=F9B8C942-367E-433A-9A71-40C5F2806A74&ext=JPG (index.js, line 49)
[Log] resolveLocalFileSystemURL returned:  cdvfile://localhost/assets-library/asset/asset.JPG?id=F9B8C942-367E-433A-9A71-40C5F2806A74&ext=JPG (index.js, line 18)
[Log] copyTo returned:  file:///var/mobile/Applications/9C838867-30BE-4703-945F-C9DD48AB4D64/Documents/myFolder/myImage.JPG (index.js, line 36)
[Log] DEPRECATED: Update your code to use 'toURL' (Entry.js, line 202)

, URL:

  • URL assets-library://
  • resolveLocalFileSystemURL, URL cdvfile://, , .
  • file:/// URL-, . URL . ( toNativeURL() URL, 1.1.0)

URL- iOS , <img>.

+1

Source: https://habr.com/ru/post/1541552/


All Articles