拍摄照片并将其保存在特定文件夹PhoneGap中

时间:2016-06-22 09:12:31

标签: javascript android cordova camera filesystems

我正在开发一个带有PhoneGap的Android应用程序,我的问题是我想要拍照并保存在指定的文件夹中,我在线阅读了很多教程,但我找不到解决方案,现在我拍照并将其保存在默认文件夹中。

这是我的 js 文件。

var pictureSource;   
var destinationType;
document.addEventListener("deviceready",onDeviceReady,false);

function onDeviceReady() {
   pictureSource=navigator.camera.PictureSourceType;
   destinationType=navigator.camera.DestinationType;
}

function onPhotoDataSuccess(imageURI) {

   var smallImage = document.getElementById('smallImage');
   smallImage.style.display = 'block';
   smallImage.src = imageURI;
   movePic(imageURI);
}

function capturePhoto() {
   navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
   destinationType: destinationType.FILE_URI,
   saveToPhotoAlbum: true});
}

function onFail(message) {
   alert('Failed because: ' + message);
}

function movePic(file){
   window.resolveLocalFileSystemURI(file, resolveOnSuccess, resOnError);
}

function resolveOnSuccess(entry){
   var d = new Date();
   var n = d.getTime();

   var newFileName = n + ".jpg";
   var myFolderApp = "Geofolder";

   window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys)  {  
        //The folder is created if doesn't exist
    var direct = fileSys.root;
          direct.getDirectory( myFolderApp,
            {create:true, exclusive: false},
            function(myFolderApp) {
                entry.moveTo(myFolderApp, newFileName,  successMove,  resOnError);
            },
            resOnError);
    },
    resOnError);
}

function successMove(entry) {
   sessionStorage.setItem('imagepath', entry.fullPath);

}

function resOnError(error) {
   alert(error.code); 

1 个答案:

答案 0 :(得分:1)

您必须获取目录条目。请尝试以下函数。

function moveFile(fileUri) {
window.resolveLocalFileSystemURL(
      fileUri,
      function(fileEntry){
            window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory,
                function(dirEntry) {
                        fileEntry.moveTo(dirEntry, "fileName.jpg", function(entry){
                            alert("File moved.check internal memory");
                        },resOnError);
                    },
                    resOnError);
      },
      resOnError);}