In an Android Studio / Gradle Android project, I have two build variants, Debug And Release (the standard project setup).
You can see my folder structure in this picture:
I have a WebView that should display the imprint.html from the /res/raw-folder. This works in teh release build, but not the debug build, the WebView says
Couldn't load website under 'file///android_res/raw/imprint.hml
net::EE_FILE_NOT_FOUND
which puzzles me.
What am I doing wrong?
Try removing the suffix to your debug mode.
Also, please make sure in proguard rules you have
-keepclassmembers class **.R$* {public static <fields>;} -keep class **.R$*
EDIT: After figuring out the problem (the debug app package suffix ".debug"), try to load the file this way:
String path = "android.resource://" + getPackageName() + "/" + R.raw.my_web_file; Uri myFileUri = Uri.parse(path); File file = new File(myFileUri.toString()); webview.loadUrl(file.getAbsolutePath());
2nd EDIT: If that still doesn't work (although for me it works), try this: load the file to the webview using an input stream.
String prompt = ""; try { InputStream inputStream = getResources().openRawResource(R.raw.my_web_file); byte[] buffer = new byte[inputStream.available()]; inputStream.read(buffer); prompt = new String(buffer); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } webview.loadData(prompt,"text/html","utf-8");
Try this one it works for me.
WebView webview = (WebView) findViewById(R.id.webview); webview.loadUrl("file:///android_res/raw/filename.html");
As you may have realised, the raw directory was not intended for direct path access (see docs) and the assets directory is preferred.
From API level 8 you could try this slightly more verbose method, but I've had mixed success:
WebView webview = ...;
String path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.imprint);
webview.loadUrl(path);
Alternatively the res path should work with the 3 slashes and colon both present, if it isn't working for Debug, there could be a problem with how your debug version is compiled, you should show/check your gradle file
webview.loadUrl("file:///android_res/raw/imprint.html");
I think you can do the same thing by using android_asset folder, instead of raw folder. Just put all your .html files in to asset.
Syntax is as below :
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
wv.loadUrl("file:///**android_asset**/xyz.html");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With