如何使用phonegap应用程序获取所选图像的URL [英] How to get the URL of a selected image using phonegap application

查看:63
本文介绍了如何使用phonegap应用程序获取所选图像的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用camera.getPicture()方法打开照片库,并让用户选择图像.这是我的参考

I’m using camera.getPicture() method to open up the photo library and let the user select an image. this is my reference

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

我们可以将图像作为base64字符串获取,也可以获取其URI并在函数onPhotoURISuccess(imageURI)中在我们的应用程序中使用. 我选择URI是因为它很容易处理.我被卡住是因为图片始终以jpeg格式返回,甚至无法测量图片的fileSize.我是通过调试来跟踪URI的,它始终与下面的

We can get the image as a base64 string or can get its URI and use in our application in from the function onPhotoURISuccess(imageURI). I select the URI because It is easy for me to process. I got stuck because the image is returned always as jpeg and even the image’s fileSize cannot be measured. I traced the URI by debugging and it’s always similar to the below

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

它正在尝试将图像保存在本地并拍摄任何扩展名为jpg的图像. 我想知道如何解决此问题.有人对如何通过这种方法获取图片的网址有任何想法吗?

It’s trying to save the image locally and takes any image with the extension of jpg. I’m wondering how to solve this issue.Does anyone have any idea on how we can get the URL of the image from this approach?

推荐答案

由于我没有得到答案,所以我继续研究了phoneGap文档,最后得到了答案.以下是我的代码示例.请注意,文件扩展名识别方法来自此处

Since I didn't get an answer I went on digging the phoneGap documentation and finally got the answer. following is my code samples. note that the file extension identifying method is taken from here

我的html(在phoneGap应用程序中)页面上有一个按钮控件,如下所示 任何人都可以从此处来参考phoneGap文档.太有用了.我使用的是2.9.0版

I have a button control in my html (in the phoneGap application) page as below anyone can refer the phoneGap documentation from here. It's so helpful.I have used version 2.9.0

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

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

and in my javascript, I have the function getPhoto() as below

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 });
    }

一旦选择了一张照片,该代表将被称为

once, a photo is selected, this delegate will be called

   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()函数在本机端异步调用文件扩展名检查方法.为了简单起见,我只发布了应用程序逻辑部分

checkFileExtention() function asynchronously calls the method of file extension checking at the native side. For the simplicity, I post only the application logic part

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

When getting the FILE_URI it saves the image locally and sends a URI which looks like below file://localhost/Users/user/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/B50E3AD6-74F8-43F6-9F91-F28D2B06DF62/tmp/cdv_photo_116.jpg

显然,文件类型是隐藏的,因为它始终表示为jpg

apparently, the file type is hidden because it's always indicating as jpg

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

when NATIVE_URI is used, it looks like this assets-library://asset/asset.JPG?id=5CF16D20-9A80-483A-AC1C-9692123610B1&ext=JPG note that above uri shows JPG becasue the image is JPG, else the real file extension of the selected image is shown.

最后,如果将本地uri发送到contentTypeForImageData函数,它将为我提供正确的输出

ultimately, if when the native uri is sent to the contentTypeForImageData funtion, it gives me the correct output

这篇关于如何使用phonegap应用程序获取所选图像的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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