Reputation: 2007
I have a WebView
that just displays an image from the external SD-card. This works well, however if the image is overwritten with new data, the WebView
does not update the image.
This is even more curious because the WebView
is created totally new in the onCreate
method of the activity. Here is my code:
@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();
}
The image at the path that is saved in imagePath
is overwritten by another activity before the path is sent to this activty. The WebView
however still shows the old image data.
As you can see I already tried to set the cache mode and I tried to manually reload the WebView
, with no luck unfortunately.
I also checked with a file manager and the image on the SD-card is overwritten with new data. I tried to overwrite the file multiple times, but that doesn't work either. Somehow the image data gets cached somewhere or something. How can I avoid this and load the new data every time?
(Obviously creating a new file with a different filename works fine, but I want to replace the old file.)
Upvotes: 2
Views: 1822
Reputation: 513
I'm not too familiar with Webview. but in ordinary websites you can use the querystring to emulate a unique adress, while still loading same image. this is often used on webpages on css files.
example: http://www.webpage.com/image.jpg?cachekey=23456456754 by randomizing the cachekey every time the image loaded, it is treated as a unique image and does not load from cache.
Put a randomized int or string in the end of your filename string
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>";
I have not tested this yet. but it's an idea how to solve it.
Upvotes: 6