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

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

问题描述

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

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:

我有一个 WebView,它应该显示/res/raw-folder 中的 imprint.html.WebView 说,这适用于发布版本,但不适用于调试版本

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

这让我很困惑.

我做错了什么?

推荐答案

尝试删除调试模式的后缀.

Try removing the suffix to your debug mode.

另外,请确保您在 proguard 规则中有

Also, please make sure in proguard rules you have

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

找出问题后(调试应用程序包后缀.debug"),尝试以这种方式加载文件:

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

第二次如果这仍然不起作用(尽管对我来说它有效),请尝试以下操作:使用输入流将文件加载到 webview.

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

这篇关于/res/raw 下的文件在 Debug buildvariant 中无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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