Reading image capture files in PhoneGap

I am working on a PhoneGap application that captures images using a camera and then downloads them. There are two camera modes in PhoneGap: raw base64 encoded data or a file URI.

The documents themselves say:

Note. The image quality of images taken with the camera on a newer device is not bad. Encoding such images using Base64 has caused memory problems on some of these devices (iPhone 4, BlackBerry Torch 9800). Therefore, using FILE_URI as "Camera.destinationType" is highly recommended.

Therefore, I really want to use the FILE_URI parameter. This works great, and you can even display images in IMG tags. The url is as follows:

File: //localhost/var/mobile/Applications/4FE4642B-944C-449BB-9BD6-1E442E47C7CE/tmp/photo_047.jpg

However, at some point I want to read the contents of the file to upload to the server. I was going to do this using the FileReader type. This does not work, and I think because I can’t access the file at the above URL.

The error code returned from readDataUrl is 1> FileError.NOT_FOUND_ERR = 1;

Any ideas how I can get to the file? I tried just to access the last part of the path (photo_047.jpg) based on another sample that I saw, but no luck.

+5
source share
4 answers

I'm just starting out with PhoneGap, and given the age of this question, you may have already found the answer, but I will give it a try anyway.

-, FileTransfer? : URI.

FileTransfer , , PhoneGap , FileReader, . , - URI - . , NOT_FOUND_ERR, , file:/localhost/var....

URI:

var path = /file:\/\/.*?(\/.*)/.exec(fileuri)[1];

, !

+1

jgarbers , . , Temp Document. .

window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, ...
+1

...

window.requestFileSystem (LocalFileSystem.TEMPORARY, 0, ...

...

var path = / file: //.? (/.)/.exec(fileuri) [1];

Ref. above jgarbers and Rik answers (solution was successfully tested on iOs 7)

0
source

You can connect the file transfer plugin to upload any file to the server.

//// pass your file uri to the mediafie param
function uploadFile(mediaFile) {
    var ft = new FileTransfer();
    path = mediaFile.fullPath;
    name = mediaFile.name;
////your service method url
    var objUrl = http://example.com;


    ft.upload(path,
        objUrl,
        function (result) {
            alert("Success");


        },
        function (error) {
            alert('Error uploading file ' + path + ': ' + error.code);
        },
        { fileName: name });
}
0
source

All Articles