Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...