File under / res / raw is not available in Debug buildvariant

In the Android / Gradle Android Android project, I have two build options: Debug And Release (standard project setup).

You can see my folder structure in this image:

enter image description here

I have a WebView that should display imprint.html from / res / raw -folder. This works in release builds, but not in debug builds, 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?

+22
android android-studio gradle
Nov 06 '14 at 10:49
source share
6 answers

Try removing the suffix in debug mode.

Also, make sure that in the proguard rules you have

 -keepclassmembers class **.R$* {public static <fields>;} -keep class **.R$* 

EDIT: After finding out the problem (suffix of the debug package ".debug"), try downloading the file as follows:

  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 this still does not work (although it works for me), try this: upload the file to webview using the 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"); 
+7
Feb 26 '16 at 8:43
source share

Try this, it works for me.

 WebView webview = (WebView) findViewById(R.id.webview); webview.loadUrl("file:///android_res/raw/filename.html"); 
+2
Feb 17 '16 at 9:39
source share

As you can see, the source directory is not intended for direct access to the path ( see documents ), and the resource directory is preferable.

From API level 8, you can try this somewhat more detailed method, but I 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 three slashes and a colon present, if it does not work for Debug, there may 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"); 
+2
Feb 23 '16 at 15:44
source share

I think you can do the same using the android_asset folder instead of the original folder. Just put all your .html files in the asset.

The syntax is as follows:

 setContentView(R.layout.webview); webView = (WebView) findViewById(R.id.webView1); wv.loadUrl("file:///**android_asset**/xyz.html"); 
+1
Jul 07 '15 at 8:05
source share

I had the same problem, and the reason for this was a different applicationId debugging taste. Based on DDsix's answer, this is what worked best for me.

It processes:

  • Different applicationId in different build options
  • Special characters in HTML (รฅรครก)
  • Localization of HTML file in different source folders (raw, raw-en, raw-xyz)

the code

 InputStream inputStream = getResources().openRawResource(rawFileResId); StringBuilder stringBuilder = new StringBuilder(); try { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { stringBuilder.append(line); } } catch (IOException e) { // Log Exception } finally { try { inputStream.close(); } catch (IOException e) { // Log Exception } } String html = stringBuilder.toString(); webView.loadData(html, "text/html; charset=utf-8", "UTF-8"); 

Use loadDataWithBaseURL() instead of loadData() if you are using CSS, which is also in the raw folder

 mWebView.loadDataWithBaseURL("file:///android_res/raw/", html, "text/html; charset=utf-8", "UTF-8", null); 
0
Apr 27 '17 at 16:21
source share

try it

 WebView webview = (WebView) findViewById(R.id.webview); webview.loadUrl("file:///android_res/raw/filename.html"); 
-2
Feb 25 '16 at 11:14
source share



All Articles