在debug buildvariant中无法访问/ res / raw下的文件 [英] File under /res/raw not accessible in Debug buildvariant

查看:171
本文介绍了在debug buildvariant中无法访问/ res / raw下的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android Studio / Gradle Android项目中,我有两个构建变体,Debug和Release(标准项目设置)。



您可以在此图片中看到我的文件夹结构: $ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆