webview无法从sd卡加载html文件 [英] webview cannot load html file from sd card
本文介绍了webview无法从sd卡加载html文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从 SD 卡加载 html 文件.笔记:-> 如果我加载 http://www.google.com 就可以了.-> 文件存在-> 我有互联网和 WRITE_EXTERNAL_STORAGE 权限
I'm trying to load a html file from sd-card. Note: -> if i load http://www.google.com it works. -> the file exists -> i have permissions for internet and WRITE_EXTERNAL_STORAGE
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addB = (Button) findViewById(R.id.add);
webComp = (WebView) findViewById(R.id.webC);
WebSettings webSettings = webComp.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(false);
webSettings.setAllowFileAccess(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setSavePassword(false);
webSettings.setSaveFormData(false);
webSettings.setJavaScriptEnabled(true);
webComp.setWebViewClient(new HelloWebViewClient());
webComp.loadUrl("/sdcard/FMS/1/message.html");
}
谢谢!:)
推荐答案
Misca,
您不应该像那样硬编码 SD 卡的目录.它通常位于 /mnt/sdcard/
,但这永远无法保证.您还应该始终检查 sdcard 是否存在并首先安装!
You shouldn't hard code the directory of the sdcard like that. Its typically at /mnt/sdcard/
but this is never assured. You should also always check if the sdcard exists and is mounted first!
您想使用以下内容:
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Log.d(TAG, "No SDCARD");
} else {
webComp.loadUrl("file://"+Environment.getExternalStorageDirectory()+"/FMS/1/message.html");
}
这篇关于webview无法从sd卡加载html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文