将使用相机拍摄的照片捕获并存储到本地数据库/ PhoneGap / Cordova / iOS中

我目前正在使用Phonegap / Cordova和jQuerymobile构buildiOS应用程序。 这个想法是用相机拍摄照片并存储拍摄的图像以供将来使用。 我想将path/文件名存储到我的本地数据库,并将图片文件移动到iPhone中的一个永久的地方。

有人能给我一个例子吗?

好的,这是解决scheme。

  • 在Html文件中

  • 我有一个图片标签用于显示相机拍摄的照片:

  • 我有一个运行拍照function的button:捕捉照片

  • 捕捉照片的function是(拍摄照片时,“smallImage”ID的scr填充照片的path)

    function capturePhoto() { // Take picture using device camera and retrieve image as base64-encoded string navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50 }); } //Callback function when the picture has been successfully taken function onPhotoDataSuccess(imageData) { // Get image handle var smallImage = document.getElementById('smallImage'); // Unhide image elements smallImage.style.display = 'block'; smallImage.src = imageData; } //Callback function when the picture has not been successfully taken function onFail(message) { alert('Failed to load picture because: ' + message); } 
  • 现在我想将图片移动到永久文件夹中,然后将链接保存到我的数据库中:

     function movePic(file){ window.resolveLocalFileSystemURI(file, resolveOnSuccess, resOnError); } //Callback function when the file system uri has been resolved function resolveOnSuccess(entry){ var d = new Date(); var n = d.getTime(); //new file name var newFileName = n + ".jpg"; var myFolderApp = "EasyPacking"; window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) { //The folder is created if doesn't exist fileSys.root.getDirectory( myFolderApp, {create:true, exclusive: false}, function(directory) { entry.moveTo(directory, newFileName, successMove, resOnError); }, resOnError); }, resOnError); } //Callback function when the file has been moved successfully - inserting the complete path function successMove(entry) { //I do my insert with "entry.fullPath" as for the path } function resOnError(error) { alert(error.code); } 
  • 我的文件已被保存在数据库中显示它,我把包含图像src行的“file://”前面

希望这个帮助。 J.

PS: – 非常感谢Simon Mac Donald(http://hi.im/simonmacdonald)在google上发表的博文。

杰罗姆的答案就像一个魅力..唯一要指出的人,当他们想要使用该文件不使用entry.fullPath,因为这有时会导致问题,使用entry.toURL(),因为这会给你满path而不必将“file://”添加到path以及绕过SD卡存储等问题。

 if (index == '0') { var options = { quality: 50, destinationType: Camera.DestinationType.FILE_URI, sourceType: Camera.PictureSourceType.CAMERA, allowEdit: false, encodingType: Camera.EncodingType.JPEG, popoverOptions: CameraPopoverOptions, saveToPhotoAlbum: false, correctOrientation:true }; $cordovaCamera.getPicture(options).then(movePic,function(imageData) { $rootScope.imageUpload=imageData; }, function(err) { console.error(err); }); function movePic(imageData){ console.log("move pic"); console.log(imageData); window.resolveLocalFileSystemURL(imageData, resolveOnSuccess, resOnError); } function resolveOnSuccess(entry){ console.log("resolvetosuccess"); //new file name var newFileName = itemID + ".jpg"; var myFolderApp = "ImgFolder"; window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) { console.log("folder create"); //The folder is created if doesn't exist fileSys.root.getDirectory( myFolderApp, {create:true, exclusive: false}, function(directory) { console.log("move to file.."); entry.moveTo(directory, newFileName, successMove, resOnError); console.log("release"); }, resOnError); }, resOnError); } function successMove(entry) { //I do my insert with "entry.fullPath" as for the path console.log("success"); //this is file path, customize your path console.log(entry); } function resOnError(error) { console.log("failed"); } } 

我刚刚根据Jérôme的回答做了一个有承诺的工作:

 function moveFile(file){ var deferred = $q.defer(); window.resolveLocalFileSystemURL(file, function resolveOnSuccess(entry){ var dateTime = moment.utc(new Date).format('YYYYMMDD_HHmmss'); var newFileName = dateTime + ".jpg"; var newDirectory = "photos"; window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) { //The folder is created if doesn't exist fileSys.root.getDirectory( newDirectory, {create:true, exclusive: false}, function(directory) { entry.moveTo(directory, newFileName, function (entry) { //Now we can use "entry.toURL()" for the img src console.log(entry.toURL()); deferred.resolve(entry); }, resOnError); }, resOnError); }, resOnError); }, resOnError); return deferred.promise; } function resOnError(error) { console.log('Awwww shnap!: ' + error.code); } 

有3个步骤:

  1. 获取照片。 苹果公司在iPhone开发网站上有一个很好的例子
  2. 获取你的文档目录,如下所示:
     NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); NSString *docDir = [arrayPaths objectAtIndex:0]; 
  3. 最后,将步骤1中获得的UIImage存储到磁盘中:
     NSString *filename = @"mypic.png"; NSString *fullpath = [docDir stringByAppendingPathComponent:filename]; [UIImagePNGRepresentation(image) writeToFile:fullpath atomically:YES];