【问题标题】:Android: how to read html files from internal storage and load in webview [closed]Android:如何从内部存储中读取 html 文件并在 webview 中加载 [关闭]
【发布时间】:2015-02-26 05:42:25
【问题描述】:

我搜索了很多,但没有解决我的问题。我想构建一个从选项卡的内部文件夹中读取 html 文件的应用程序。 html文件的路径是/file manager/device storage/Android/data/K1.html。
问题是网页的数量可能会因用户而增加或减少,我想构建应用程序可以读取该文件夹中所有网页的功能。我使用以下代码从内部存储读取文件,但它不指向路径。我知道这并不难,请指导我如何获得此功能,在此先感谢。

File tabFolder = getFilesDir();
String root = tabFolder.toString();
File myFile = new File(root + "/Android/data/K1.html");
    if(myFile.exists()){
    Toast.makeText(MainActivity.this, "exists", Toast.LENGTH_SHORT).show();

    }
    else{
        Toast.makeText(MainActivity.this, "does not exists", Toast.LENGTH_SHORT).show();
    } 

【问题讨论】:

  • 您是否在清单文件中设置了权限?
  • 是的,我使用了这个权限,但它用于外部存储 还有其他内部存储权限吗?

标签: java android html webview


【解决方案1】:

立即尝试...

String fileName = "K1.html";
String content = "K1 html file content here";
FileOutputStream outputStream = null;
try {
outputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
outputStream.write(content.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();}

String path=getApplicationContext().getFilesDir().getAbsolutePath()+"/K1.html";
File file = new File ( path ); 
if ( file.exists() ) 
{
 // Toast File is exists
}
else
{
 // Toast File is not exists
}

【讨论】:

  • 对不起,我必须使用内部存储。难道不能用内部存储吗?
  • getAbsolutePath 指向这个路径 /data/data/com.example.html/files/K1.html 并且'files'文件夹是空的,这就是为什么它不读取文件,因为k1。此路径中不存在 html.. 请您建议我如何将文件放在此文件夹中,我试过但我无法在此处粘贴 K1.html。
  • 检查我更新的答案...
  • 它有效,伙计。太感谢了。你能告诉我如何使用这种方法在 webview 中加载 html 文件 webView.loadUrl("file:///" + path);
  • 你用 webView 完成了吗...?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-29
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 2021-12-09
  • 2013-01-23
相关资源
最近更新 更多