Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

android - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...