【问题标题】:Take a Picture and Save it in a Specific Folder PhoneGap拍照并将其保存在特定文件夹中 PhoneGap
【发布时间】:2016-06-22 09:12:30
【问题描述】:

我正在使用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); 

【问题讨论】:

标签: javascript android cordova camera filesystems


【解决方案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);}

【讨论】:

    猜你喜欢
    • 2011-10-17
    • 2012-11-25
    • 1970-01-01
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多