图像webview缓存显示没有互联网连接的图像

时间:2017-07-26 15:00:01

标签: php android caching webview carousel

我有一个php webapge,它显示带有自动轮播的图像,以及一个显示带有该网页的WebView的Android应用程序。

我想缓存在我的轮播中运行的图像,但只有1或2个第一张图像被缓存,而其他图像则是“未找到图像”。

我如何缓存图像,这样我就能在没有有效网络连接的Android设备上看到它们?

显示图片:

    var state = 0;    // 0: load 1: change content 2: finished
var passedsec = 0;

var dispId = 0;
var dispType = "image";
var prevType = "image";
var loadType = "image";
var duration = 5;

var i = 0;

var frame = {
    "image": ["#img1", "#img2"]
};

$(window).on('load', function() {
    $(frame[dispType][dispId]).css("display", "block");
});

$(document).ready(function(){
    function loadNews(data) {
        newsList = data;
    }

    var newsList = Array();
    var newsOrder = 0;
    var newsElm = ["#news0", "#news1"];
    var newsState = 0;
    var newsWidth = $(".news").width();

    $.getJSON("libs/json/news.json", null, loadNews);

    $(newsElm[newsState]).css({right:(newsWidth), display:'block',opacity:'0'}).velocity({right: '10', opacity:'1'}, "1000", "swing");

    setInterval(function(){
        $(newsElm[newsState]).velocity({right:(-(newsWidth)),opacity:'0'}, 1000, "swing");

        newsState = 1 - newsState;

        $(newsElm[newsState]).text(newsList[newsOrder][0]);
        newsOrder++;

        if(newsOrder == newsList.length){
            $.getJSON("libs/json/news.json", null, loadNews);
            newsOrder = 0;
        }

        $(newsElm[newsState]).css({right:(newsWidth),display:'block',opacity:'0'}).velocity({right: '10', opacity:'1'}, "1000", "swing");
    }, 10000);

    // //////////////////////////////////////////////////////////

    var id = getUrlParameter('bid');

    // //////////////////////////////////////////////////////////

    $.get("libs/get/masterGallery.php", {bid: id}, function( data ) {
        if(data != 0) {
            $.getJSON("libs/json/" + data + "-master.json", null, loadContent);
        } else {
            $.getJSON("libs/json/" + id + ".json", null, loadContent);
        }
    });

    var myInt = setInterval(function(){
        passedsec++;

        if(state == 0) {
            loadType = contentList[i]["type"];
            if(typeof(loadType) == "undefined") {
                clearInterval(myInt);
            }

            $(frame[loadType][1 - dispId]).attr('src', contentList[i]["url"]);

            state = 1;
            if(i + 1 == contentList.length){
                    $.get("libs/get/masterGallery.php", {bid: id}, function( data ) {
                if(data != 0) {
                    $.getJSON("libs/json/" + data + "-master.json", null, loadContent);
                } else {
                    $.getJSON("libs/json/" + id + ".json", null, loadContent);
                }
            });
            }
        } else if(state == 1) {
            if(passedsec > duration - 1) {
                state = 2;
                passedsec = 0;

                dispId = 1 - dispId;
                dispType = loadType;

                $(frame[dispType][dispId]).css("display", "block");
                $(frame[prevType][1 - dispId]).css("display", "none");

                prevType = dispType;
                duration = contentList[i]["duration"];

                if(i + 1 == contentList.length) i = 0; else i++;
            }
        } else if(state == 2) {
            if(passedsec >= 1) {
                state = 0;
                passedsec = 0;
            }
        }
    }, 1000);
});

在android上缓存网站:

if (!isNetworkAvailable()) { // loading offline
                    webb.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
                    System.out.println("WEBSITE OFFLINE");
                } else {
                    webb.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); // load online by default
                    System.out.println("WEBSITE ONLINE");
                }

                webb.loadUrl("http://******/manage/showadv.php?bid=" + ID);

                new ImAlive().execute(ID, "site");

                System.out.println("WEBSITE RELOADED.");

非常感谢。

0 个答案:

没有答案