简体   繁体   English

带有jQuery的Phonegap更改图像src在Android上不起作用

[英]Phonegap change image src with jquery not working on Android

I try to change the src of a img tag with jquery. 我尝试用jquery更改img标签的src。 In firefox it is working fine, but in the phonegap developer app on android, nothing happens. 在firefox中,它工作正常,但是在android上的phonegap开发人员应用中,什么也没有发生。

What I'm doing: 我在做什么:

I'm getting an image as base64 with a ajax request. 我正在使用ajax请求将图像作为base64。 If the request is complete, I'm making an URL Object from the image, and change the src of the img tag to the url Object. 如果请求完成,则从图像中创建一个URL对象,然后将img标签的src更改为url对象。 Here my code: 这是我的代码:

        $.ajax({
        type: 'GET',
        dataType: 'json',
        url: MySecretPHPFunctionOnAServerThatReturnsABase64Image..., 
        complete: function(data) {
            var base64Image = data.responseText;
            var image = makeUrlObject(base64Image, "image/jpeg");

            // ERROR!!! :-)
            // Only working in Browser, not on android... 
            $("#scanPreview").prop("src", image  + '?' + genTimestamp());
        },
        error: function() {}
    });

I think the makeUrlObject Function is not the reason for the error, but if you want to see it, for making sure, or if I'm overlooking something ;-) 我认为makeUrlObject函数不是错误的原因,但是如果您想查看它,以确保它,或者我忽略了某件事;-)

    function makeUrlObject(dataURL, typeURL) {        
        var binStr = atob(dataURL);
        var buf = new ArrayBuffer(binStr.length);
        var view = new Uint8Array(buf);
        for(var i = 0; i < view.length; i++)
            view[i] = binStr.charCodeAt(i);

        var blob = new Blob([view], {type: typeURL});

        binStr=null;
        buf = null;
        view = null;

        URL = window.URL || window.webkitURL;

        return URL.createObjectURL(blob);    
   };

Instead of .prop("src" 代替.prop(“ src”

try 尝试

var elem = document.getElementById('myimg'); var elem = document.getElementById('myimg'); myimg.src="theimage.jpg"; myimg.src =“ theimage.jpg”;

also case sensitive 也区分大小写

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM