在debug buildvariant中无法访问/ res / raw下的文件 [英] File under /res/raw not accessible in Debug buildvariant
问题描述
您可以在此图片中看到我的文件夹结构: $ b
我有一个WebView,应该显示imprint.html从/ res /原始文件夹。
这适用于发布版本,但不适用于调试版本,WebView说
无法加载网站'file /// android_res / raw / imprint.hml $ b $ net :: EE_FILE_NOT_FOUND
其中困惑我。
我做错了什么?
尝试删除后缀到您的调试模式。
另外,请确保您有proguard规则
-keepclassmembers class ** .R $ * {public static< fields> ;;}
-keep class ** .R $ *
编辑:
在找出问题后(调试应用程序包后缀.debug),尝试以这种方式加载文件:
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());
第二编辑:
如果仍然不起作用(尽管对我而言) ,试试这个:使用输入流将文件加载到webview。
String prompt =;
尝试{
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(提示符,text / html,utf-8);
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");
这篇关于在debug buildvariant中无法访问/ res / raw下的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!