0

我在 Worklight v6 中构建了一个使用 cordova API 2.6 版的工作灯应用程序

我使用了在此位置提供的示例http://docs.phonegap.com/en/2.6.0/cordova_camera_camera.md.html#Camera

navigator.camera.getPicture(OnSuccess,OnFail, {quality:50,destinationType:Camera.DestinationType.NATIVE_URI,sourceType:Camera.PictureSourceType.CAMERA,saveToPhotoAlbum:true});

即使我使用 NATIVE_URI 结果,我在 OnSuccess 方法中得到的结果也是 file:// uri,而不是文档上写的 content://uri。

Camera.DestinationType = {
DATA_URL : 0,                // Return image as base64 encoded string
FILE_URI : 1,                // Return image file URI
NATIVE_URI : 2               // Return image native URI (eg. assets-library:// on iOS   or content:// on Android)

};

我已在清单 xml 文件中添加了所有必需的权限。

    <uses-permission android:name="android.permission.CAMERA"/>  
    <uses-feature android:name="android.hardware.camera"/>

在此处查看完整代码:

<!DOCTYPE HTML>
<html>
        <head>
            <meta charset="UTF-8">
            <title>testProject</title>
            <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
            <link rel="shortcut icon" href="images/favicon.png">
            <link rel="apple-touch-icon" href="images/apple-touch-icon.png">
            <link rel="stylesheet" href="css/testProject.css">
            <script>window.$ = window.jQuery = WLJQ;</script>
                <script type="text/javascript" charset="utf-8">

    var pictureSource;   // picture source
    var destinationType; // sets the format of returned value 

    // Wait for Cordova to connect with the device
    //
    document.addEventListener("deviceready",onDeviceReady,false);

    // Cordova is ready to be used!
    //
    function onDeviceReady() {
        pictureSource=navigator.camera.PictureSourceType;
        destinationType=navigator.camera.DestinationType;
    }


    // Called when a photo is successfully retrieved
    //
    function onPhotoURISuccess(imageURI) {
      // Uncomment to view the image file URI 
      alert(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;
    }



    // A button will call this function
    //
    function getPhoto(source) {
      // Retrieve image file location from specified source
      navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, 
        destinationType: destinationType.FILE_URI,
        sourceType: source,saveToPhotoAlbum:true });
    }

  function getPhoto2(source) {
      // Retrieve image file location from specified source
      navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, 
        destinationType: destinationType.NATIVE_URI,
        sourceType: source,saveToPhotoAlbum:true });
    }
    // Called if something bad happens.
    // 
    function onFail(message) {
      alert('Failed because: ' + message);
    }

    </script>

        </head>
        <body id="content" style="display: none;">

        <button onclick="getPhoto(pictureSource.CAMERA);">Camera</button><br>
        <button onclick="getPhoto2(pictureSource.CAMERA);">From Photo Album</button><br>
        <img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
        <img style="display:none;" id="largeImage" src="" />


            <!--application UI goes here-->
            testProject
            <script src="js/initOptions.js"></script>
            <script src="js/testProject.js"></script>
            <script src="js/messages.js"></script>
        </body>
</html>
4

1 回答 1

0

尽管设置了 NATIVE_URI,我目前不确定为什么 Cordova 会返回 FILE_URI,但请注意,无论如何都鼓励使用 FILE_URI:

http://docs.phonegap.com/en/2.6.0/cordova_camera_camera.md.html#camera.getPicture

注意:在较新的设备上使用相机拍摄的照片的图像质量非常好,即使指定了质量参数,相册中的图像也不会被缩小到较低的质量。使用 Base64 编码此类图像会导致许多新设备出现内存问题。因此,强烈建议使用 FILE_URI 作为“Camera.destinationType”。


如果您坚持使用content://架构,也许以下内容会有所帮助:Get content uri from file path in android

于 2013-08-25T16:56:59.460 回答