2012-11-27 17 views
2

我有一個WebView只顯示來自外部SD卡的圖像。這很有效,但是如果圖像被新數據覆蓋,則WebView不會更新圖像。WebView加載過時的本地圖像。如何更新?

這更令人好奇,因爲WebView是在onCreate活動方法中全新創建的。這裏是我的代碼:

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    Log.d(TAG, "Creating Viewer"); 
    super.onCreate(savedInstanceState); 

    Intent intent = getIntent(); 
    Bundle extras = intent.getExtras(); 

    String imagePath = extras.getString("imgFile"); 

    shareImage(Uri.fromFile(new File(imagePath))); 

    setContentView(R.layout.viewer_test); 

    WebView viewer = (WebView) findViewById(R.id.imageViewer); 
    viewer.getSettings().setAllowFileAccess(true); 
    viewer.getSettings().setBuiltInZoomControls(false); 
    viewer.getSettings().setLoadWithOverviewMode(true); 
    viewer.getSettings().setUseWideViewPort(true); 
    viewer.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); 
    String filename = "file://"+ imagePath; 
    String html = "<html><head></head><body bgcolor=\"#000000\"><center><img src=\""+ filename + "\"></center></body></html>"; 
    viewer.loadDataWithBaseURL(null, html, "text/html","utf-8", null); 
    viewer.reload(); 
} 

在被保存在imagePath路徑中的圖像是由另一活動覆蓋之前的路徑發送到這個activty。然而WebView仍然顯示舊的圖像數據。

正如你所看到的,我已經嘗試設置緩存模式,我試圖手動重新加載WebView,沒有幸運的遺憾。

我也用文件管理器檢查過,並且SD卡上的圖像被新數據覆蓋。我試圖多次覆蓋該文件,但這也不起作用。不知何故,圖像數據被緩存在某處或某處。我怎樣才能避免這種情況,並每次加載新的數據?

(顯然,創建具有不同的文件名的新文件工作正常,但我想,以取代舊文件。)

回答

6

我不是太熟悉的網頁視圖。但在普通網站中,您可以使用查詢字符串來模擬獨特的地址,同時仍然加載相同的圖像。這通常用在css文件的網頁上。

示例:http://www.webpage.com/image.jpg?cachekey=23456456754 通過在每次加載圖像時隨機化緩存鍵,它將被視爲唯一的圖像,並且不會從緩存中加載。

將隨機int或字符串中的文件名字符串

Random r = new Random(); 
int randInt = r.nextInt(8000000-1000000) + 1000000; 
String query = "?cachekey=" + randInt ; 
String html = "<html><head></head><body bgcolor=\"#000000\"><center><img src=\""+ filename + query "\"></center></body></html>"; 

我沒有測試過這個尚未結束。但這是一個想法如何解決它。

+0

工程就像一個魅力。謝謝! (不能upvote你,因爲我沒有聲譽,對不起) – P1r4nh4

+0

遺憾的是錯過了。感謝您的編輯。 =) – RoggA

相關問題