Phonegap - 从相机胶卷通过路径检索照片 [英] Phonegap - Retrieve photo from Camera Roll via path

查看:134
本文介绍了Phonegap - 从相机胶卷通过路径检索照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的PhoneGap / jQuery Mobile应用程序中,我目前使用PhoneGaps的Camera API允许用户拍摄照片,这也存储在相机胶卷。我将DestinationType设置为FILE_URI并将此路径保存在本地数据库中。但是,FILE_URI是在应用关闭时销毁的临时位置的路径。我在想保存图像的名称,然后从相机胶卷检索图像。是否有一个路径可以用来稍后检索相机胶卷中的图像?

In my PhoneGap/jQuery Mobile app, I'm currently using PhoneGaps' Camera API to allow the user to take a photo, which is also stored in the Camera Roll. I'm setting DestinationType to FILE_URI and saving this path in a local db. However, FILE_URI is a path to a temporary location that is destroyed when the app is closed. I was thinking of saving the name of the image and then retrieving the image from the Camera Roll. Is there a path I can use to later retrieve the images in the Camera Roll?

我在db中将图像保存为Base64,但我有点疲倦因为 PhoneGap API文档中的此注释:

I was saving the images as Base64 in the db, but I'm a little weary of this method because of this note in the PhoneGap API Doc:


注意:使用相机在较新的
设备上拍摄的照片的图像质量非常好。使用Base64编码这样的图像已导致
内存问题在其中一些设备(iPhone 4,黑莓手机
9800)。因此,强烈建议您使用FILE_URI作为Camera.destinationType,并强烈建议您使用

Note: The image quality of pictures taken using the camera on newer devices is quite good. Encoding such images using Base64 has caused memory issues on some of these devices (iPhone 4, BlackBerry Torch 9800). Therefore, using FILE_URI as the 'Camera.destinationType' is highly recommended.

编辑:

使用@Simon MacDonald的答案。这是我的缩减代码:

Got it working with @Simon MacDonald's answer. Here is my stripped-down code:

function capturePhoto() {
    navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 25, destinationType: Camera.DestinationType.FILE_URI });
}

function onPhotoURISuccess(imageURI) {
    createFileEntry(imageURI);
}

function createFileEntry(imageURI) {
    window.resolveLocalFileSystemURI(imageURI, copyPhoto, fail);    
}

function copyPhoto(fileEntry) {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) { 
        fileSys.root.getDirectory("photos", {create: true, exclusive: false}, function(dir) { 
                fileEntry.copyTo(dir, "file.jpg", onCopySuccess, fail); 
            }, fail); 
    }, fail); 
}

function onCopySuccess(entry) {
    console.log(entry.fullPath)
}

function fail(error) {
    console.log(error.code);
}


推荐答案

FILE_URI方法,您应该使用File API将图像从临时文件夹复制到文档文件夹,并将新路径存储在数据库中。

After you retrieve the file using the FILE_URI method you should use the File API to copy the image from the temp folder to the Documents folder and store the new path in your DB.

getPicture()的结果传递给 window.resolveLocalFileSystemURI()以获取FileEntry。然后,您可以调用 FileEntry.copyTo()方法备份您的文件。

So you would take the result of your getPicture() pass it to window.resolveLocalFileSystemURI() to get a FileEntry. Then you can call FileEntry.copyTo() method to back up your file.

这篇关于Phonegap - 从相机胶卷通过路径检索照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆