How to get the URL of a selected image using the phonegap application

Im using the camera.getPicture () method to open a photo library and let the user select an image. this is my link

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

We can get the image as a base64 string or get its URI and use it in our application from the function onPhotoURISuccess(imageURI). I choose a URI because it is easy for me to handle. I am stuck because the image is always returned as jpeg, and even the fileSize image cannot be measured. I tracked the URI by debugging and always looked like below

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

It tries to save the image locally and accepts any image with the jpg extension. I am wondering how to solve this problem. Does anyone know how we can get the image url from this approach?

+1
source share
1 answer

Since I did not receive a response, I continued to dig the documentation on the phone and finally received a response. The following are my code samples. note that the method of identifying the file extension is taken from here

I have a control button on my html (in the phoneGap application), as shown below, anyone can link to the phoneGap documentation from here . This is so helpful. I used version 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() .

//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

-, , jpg

NATIVE_URI, : ? ://asset/asset.JPG ID = 5CF16D20-9A80-483A-AC1C-9692123610B1 & = JPG , uri JPG, JPG, .

, , uri contentTypeForImageData funtion,

+2

Source: https://habr.com/ru/post/1530616/


All Articles