Problem connecting Phonegap camera (DestinationType.FILE_URI)

I am developing a phone-based application that has file transfer functions. I use the camera plugin to select an image file. The code works fine with "DestinationType.DATA_URL". But I can’t access the file named "DestinationType.FILE_URI".

DestinationType.DATA_URL just provides the contents of the image file. But I have to get the image file name and file path along with its contents. Therefore, I have to use "DestinationType.FILE_URI" in the camera settings. Below is my code,

function attachFile() {
 var pictureSource=navigator.camera.PictureSourceType;
 var cameraOptions = { quality: 49 ,  destinationType:
 Camera.DestinationType.FILE_URI, sourceType: pictureSource.PHOTOLIBRARY };             
 navigator.camera.getPicture(attachSuccess, attachFail, cameraOptions);
}  

function attachSuccess(fileuri) {
 filePath = JSON.stringify(fileuri);
 console.log("FilePath: "+filePath ); 
 window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
}

function attachFail() {
 console.log("attach failed");
}

function gotFS(fileSystem) {            

  console.log("gotFS:");      
  var root = "/"+fileSystem.root.name;  
  console.log("root:  "+root); 
  filePath = filePath .substring(filePath.indexOf(root));        

  var imageName = filePath.substring(filePath.lastIndexOf('/'));
  var type = imageName.substring(filePath.indexOf('.'));
  fileSystem.root.getFile(filePath, null, gotFileEntry, fail);  

}

function fail() {
  console.log("** failed **");   
}

function gotFileEntry(fileEntry) {
  console.log("got file entry");
  fileEntry.file(gotFile, fail);
}

function gotFile(file) {
  console.log("got file");       
}

'attachFile', Get Picture, . attachSuccess. URI . URI FILE , ,

://////5490

, " " "-" URI. , . ( Kitkat Lollipop)

+4
3

API- Cordova FileSystem. :

window.resolveLocalFileSystemURL(filePath, // The file path
    function(fileEntry) { // found the file
        fileEntry.file(function(f) { // got the file
            var reader = new FileReader();

            reader.onloadend = function(evt) {
                var fileContent = reader.result;

                // Do something with the fileContent
                someImage.attr("src", fileContent); // Example
            }

            reader.readAsDataURL(f); // TODO find a way to read it straight to the right format (if there is any)
        }, function(e) { // cannot open file
            console.log("Error opening file: " + e.message);
        });
    },
    function(e) { // file not found
        console.log("Error finding file: " + e.message);
    });

fileContent 'data:' + base64. f, .

:

https://developer.mozilla.org/en-US/docs/Web/API/FileReader.readAsDataURL

https://github.com/apache/cordova-plugin-file/blob/master/doc/index.md

+1

URI, , URI. destinationType , FILE_URI.

, destinationType 1.

, Camera.DestinationType - .

P.S. - , cordova-plugin-file.

0

Your code is pretty good.

You must make sure that you have added the data of the File plugin to config.xml or not. This may cause the file information to be selected incorrectly.

<gap:plugin name="org.apache.cordova.file"/>

and / or below one if you want to move the file to another location.

<gap:plugin name="org.apache.cordova.file-transfer"/>

Take a look at this and. Both used destinationType.FILE_URIand could not process further, but as soon as the plugin data was correctly added to config.xml , it worked well.

0
source

All Articles