简体   繁体   English

在PhoneGap中读取图像捕获文件

[英]Reading image capture files in PhoneGap

I'm working on a PhoneGap application that captures images using the camera and, later, uploads them. 我正在开发一个PhoneGap应用程序,它使用相机捕获图像,然后上传它们。 There are two modes of operation for camera in PhoneGap: raw base64 encoded data or a file URI. PhoneGap中的摄像头有两种操作模式:raw base64编码数据或文件URI。

The docs themselves say: 文档本身说:

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). 使用Base64对这些图像进行编码会导致某些设备(iPhone 4,BlackBerry Torch 9800)出现内存问题。 Therefore, using FILE_URI as the 'Camera.destinationType' is highly recommended. 因此,强烈建议使用FILE_URI作为'Camera.destinationType'。

So I'm keen to use FILE_URI option. 所以我热衷于使用FILE_URI选项。 This works great and you can even show the images in IMG tags. 这很好用,您甚至可以在IMG标签中显示图像。 The URL looks like this: URL如下所示:

file://localhost/var/mobile/Applications/4FE4642B-944C-449BB-9BD6-1E442E47C7CE/tmp/photo_047.jpg 文件://localhost/var/mobile/Applications/4FE4642B-944C-449BB-9BD6-1E442E47C7CE/tmp/photo_047.jpg

However, at some point later I want to read the contents of the file to upload to a server. 但是,稍后我想读取要上传到服务器的文件内容。 I was going to do this using the FileReader type. 我打算使用FileReader类型执行此操作。 This doesn't work and I think it's because I can't access the file at the URL above. 这不起作用,我认为这是因为我无法访问上面的URL文件。

The error code I get back from readDataUrl is 1 > FileError.NOT_FOUND_ERR = 1; 我从readDataUrl返回的错误代码是1> FileError.NOT_FOUND_ERR = 1;

Any ideas how I can get to the file? 我有什么想法可以访问该文件? I tried just accessing the last part of the path (photo_047.jpg) based on another sample I saw but no luck. 我试着根据我看到的另一个样本访问路径的最后一部分(photo_047.jpg),但没有运气。

The answer from jgarbers was of help to me but it did not solve the problem. jgarbers的答案对我有帮助,但它没有解决问题。 I realized the camera stores photos in Temp folder instead of Document folder. 我意识到相机将照片存储在Temp文件夹而不是Document文件夹中。 Setting my local file system to temporary allowed it to find the correct location for the camera images. 将我的本地文件系统设置为临时文件允许它找到摄像机图像的正确位置。

window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, ...

I'm just getting started with PhoneGap, and given the age of this question you may have found an answer already, but I'll give it a try anyway. 我刚刚开始使用PhoneGap,考虑到这个问题的年龄,你可能已经找到了答案,但无论如何我都会尝试一下。

First, would you be able to use the built-in FileTransfer object? 首先,您是否可以使用内置的FileTransfer对象? It takes a file: URI as an argument. 它需要一个文件: URI作为参数。

If FileTransfer won't work for you, and you need to read the file data yourself, you'll need the PhoneGap File objects, like FileReader , as you said. 如果FileTransfer无法为您工作,并且您需要自己读取文件数据,那么您将需要PhoneGap 文件对象,如FileReader ,如您所述。 But most of those expect a plain pathname -- not a URI -- to specify the file to work with. 但是大多数人都希望使用普通的路径名 - 而不是URI - 来指定要使用的文件。 The reason you're getting NOT_FOUND_ERR is because it's trying to open a file named file:/localhost/var... . 你得到NOT_FOUND_ERR的原因是因为它试图打开一个名为file:/ localhost / var ...的文件

Here's a quick one-liner to extract the path part from your URI: 这是一个快速的单行程序,用于从URI中提取路径部分:

var path = /file:\\/\\/.*?(\\/.*)/.exec(fileuri)[1];

Hope this helps! 希望这可以帮助!

... ...

window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, ... window.requestFileSystem(LocalFileSystem.TEMPORARY,0,...

... ...

var path = /file://. var path = / file://。 ?(/. )/.exec(fileuri)[1]; ?(/。 )/。exec(fileuri)[1];

Ref. 参考。 above jgarbers and Rik answers (solution has been tested successfully on iOs 7) 以上jgarbers和Rik答案(解决方案已在iOs 7上成功测试)

you can user the file transfer plugin for uploading any file to the server. 您可以使用文件传输插件将任何文件上传到服务器。

//// pass your file uri to the mediafie param
function uploadFile(mediaFile) {
    var ft = new FileTransfer();
    path = mediaFile.fullPath;
    name = mediaFile.name;
////your service method url
    var objUrl = http://example.com;


    ft.upload(path,
        objUrl,
        function (result) {
            alert("Success");


        },
        function (error) {
            alert('Error uploading file ' + path + ': ' + error.code);
        },
        { fileName: name });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM