Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File under /res/raw not accessible in Debug buildvariant

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:

enter image description here

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?

like image 928
fweigl Avatar asked Nov 06 '14 10:11

fweigl


4 Answers

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"); 
like image 108
DDsix Avatar answered Sep 24 '22 16:09

DDsix


Try this one it works for me.

WebView webview = (WebView) findViewById(R.id.webview); webview.loadUrl("file:///android_res/raw/filename.html"); 
like image 29
Andy Developer Avatar answered Sep 22 '22 16:09

Andy Developer


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");
like image 37
Nick Cardoso Avatar answered Sep 24 '22 16:09

Nick Cardoso


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");
like image 24
Jaimin Modi Avatar answered Sep 25 '22 16:09

Jaimin Modi