如何使用phonegap应用程序获取所选图像的URL

我使用camera.getPicture()方法打开照片库,并让用户select一个图像。 这是我的参考

http://docs.phonegap.com/en/2.9.0/cordova_camera_camera.md.html#Camera

我们可以将图像作为base64string获取,或者可以从函数onPhotoURISuccess(imageURI)获取其URI并在我们的应用程序中使用。 我selectURI是因为我很容易处理。 我卡住了,因为图像总是以jpeg的forms返回,甚至无法测量图像的fileSize。 我通过debugging来跟踪URI,它总是和下面类似

 file://localhost/Users/user/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/B50E3AD6-74F8-43F6-9F91-F28D2B06DF62/tmp/cdv_photo_116.jpg 

它试图在本地保存图像,并以jpg的扩展名采用任何图像。 我想知道如何解决这个问题。有没有人有任何想法,我们如何可以从这种方法获取图像的URL?

由于我没有得到答案,我继续挖掘phoneGap文档,并最终得到了答案。 以下是我的代码示例。 请注意,文件扩展名识别方法是从这里取得的

我有一个button控制在我的HTML(在phoneGap应用程序)页面下面的任何人都可以从这里引用phoneGap文档。 这非常有帮助,我使用的版本是2.9.0

 <button onclick="getPhoto(Camera.pictureSource.PHOTOLIBRARY);">From Photo Library</button> 

并在我的JavaScript,我有getPhoto()函数如下

 function getPhoto(source) { // Retrieve image file location from specified source navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 100, destinationType: navigator.camera.DestinationType.NATIVE_URI, sourceType: source }); } 

一次,一张照片被选中,这个代表将被调用

  function onPhotoURISuccess(imageURI) { // Get image handle var largeImage = document.getElementById('largeImage'); // Unhide image elements largeImage.style.display = 'block'; // Show the captured photo // The inline CSS rules are used to resize the image largeImage.src = imageURI; checkFileExtention(imageURI); } 

checkFileExtention()函数asynchronous调用本机端的文件扩展名检查方法。 为了简单,我只发布应用程序逻辑部分

 //functions checks the type of the image passed - (NSString *)contentTypeForImageData:(NSString *)imageUrl { NSURL *imageURL = [NSURL URLWithString:imageUrl]; NSData *imageData = [NSData dataWithContentsOfURL:imageURL]; uint8_t c; [imageData getBytes:&c length:1]; switch (c) { case 0xFF: return @"image/jpeg"; case 0x89: return @"image/png"; case 0x47: return @"image/gif"; case 0x49: case 0x4D: return @"image/tiff"; case 0x42: return @"@image/bmp"; } return nil; } 

当获取FILE_URI时,它将在本地保存图像并发送一个如下所示的URI://localhost/Users/user/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/B50E3AD6-74F8-43F6-9F91-F28D2B06DF62 /tmp/cdv_photo_116.jpg

显然,文件types是隐藏的,因为它总是指示为JPG

当使用NATIVE_URI时,它看起来像这样assets-library://asset/asset.JPG?id = 5CF16D20-9A80-483A-AC1C-9692123610B1&ext = JPG注意上面的uri显示JPG,因为图像是JPG,否则真实文件显示所选图像的扩展名。

最终,如果当本地URI发送到contentTypeForImageData函数,它会给我正确的输出