Phonegap相机拍照

时间:2012-07-13 10:58:33

标签: java javascript cordova

我正在努力为我的移动网站添加一些移动应用专用功能。目前,该应用程序或多或少只是一个包装器。

public class Activity extends DroidGap {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.main);
        if (this.isOnline()) {
            super.loadUrl("http://mysite.com");
        } else {
            super.loadUrl("file:///android_asset/www/offline.html");
        }
    }

    public boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        }
        return false;
    }
}

问题随着以下javascript而上升。如果我替换super.loadUrl(“http://mysite.com”);使用super.loadUrl(“file:///android_asset/www/online.html”)并在那里运行以下javascript,它工作正常。但是如果我在mysite.com上运行它,它会继续抛出以下异常:

07-13 12:25:12.762:E / Web Console(28251):不允许加载本地资源:file:///data/data/com.my.app/.Pic.jpg?1342171460960 at null :0

对我做错了什么的想法?

 var camera = (function(){

        var camera = {
            settings : {
                quality: 50, 
            }
        };

        var error = function(message) {
            console.error("Error happened while trying to get a picture", message);
        };

        document.addEventListener("deviceready", function() {

            camera.settings.destinationType = navigator.camera.DestinationType.FILE_URI;    

            camera.fromPhone = function() {
                this.settings.sourceType = navigator.camera.PictureSourceType.CAMERA;
                return this;
            };

            camera.fromLibrary = function() {
                this.settings.sourceType = navigator.camera.PictureSourceType.PHOTOLIBRARY;
                return this;
            };

            camera.get  = function(callback) {
                navigator.camera.getPicture(callback, error, camera.settings);
            };

        }, false);


        return camera;

 })();

//Later, at a click event
camera.fromPhone().get(function(imageURI){
    alert("imageURI" + imageURI)
    $("#largeImage").attr("src", imageURI);
});

1 个答案:

答案 0 :(得分:1)

由于浏览器跨域安全性,您的远程加载的站点无法访问本地资源。这是一个旧线程,但解释了它的一些原因。

https://lists.webkit.org/pipermail/webkit-dev/2009-May/007658.html