【问题标题】:Loading Local Files on Android With Local HTML - net::ERR_FILE_NOT_FOUND使用本地 HTML 在 Android 上加载本地文件 - net::ERR_FILE_NOT_FOUND
【发布时间】:2019-07-27 02:25:27
【问题描述】:

将我的项目放在主根目录的文件夹中,我创建了一个简单的 hello world 项目。加载到 chrome 中的 Html 文件效果很好,但是,在主项目文件夹或子文件夹中加载文件会给出 net::ERR_FILE_NOT_FOUND

我想在后台使用基本的叠加层和按钮自动播放视频(已经设置为可以在桌面上运行)。

尝试将文件位置移动到主文件夹并将目录重命名为没有空格。

还尝试使用 file:/// 而不是通过 chrome 中的文件资源管理器打开。

在 Pixel 2XL 上使用 Android v10

<body>
<div id="mainCont">
<spread class="header">Main Test</spread><br>
<button> <a href="ALTs/L1.html">Layout 1</a></button>
</div>

<div>
  <video playsinline autoplay muted loop id="bgVideo">
    <source src="Link/VerticalTest.mp4" type="video/mp4">
  </video>
</div>
</body> 

<style>

  @font-face {
    font-family: Lato;
    src:  url(/FontRef/ProximaNova-Bold.otf) format('opentype'),
          url(/FontRef/ProximaNova-Light.otf) format('opentype'),
          url(/FontRef/ProximaNova-Reg.otf) format('opentype'),
          url(/FontRef/ProximaNova-It.otf) format('opentype'),
          url(/FontRef/ProximaNova-Sbold.otf) format('opentype'),
  }

body{
  text-align: center;
  font-family: ProximaNova-Reg, sans-serif;
}

#bgVideo {
  z-index:-1;
  position: fixed;
  right: 0;
  top: 0;
  width: 100%;
}

#mainCont{
padding: 5em;
z-index:10000;
positoin: absolute;
top:50%;
transform: translateY(100%);
}

a{
  color:white;
  text-decoration: none;
}


button{
  padding:10px;
  margin:2em;
  color: #494949 !important;
  text-transform: uppercase;
  text-decoration: none;
  background: none;
  border: 1px solid white !important;
  display: inline-block;
  transition: all 0.4s ease 0s
}
button:hover{
  color: #ffffff !important;
  background: #f6b93b;
  border-color: #f6b93b !important;
  transition: all 0.4s ease 0s;
}

.header{
font-family: ProximaNova-Sbold, sans-serif;
font-size: calc(2.87em - 1vw);
color:white;
font-weight:400;
text-transform:uppercase;
letter-spacing:.6em;
margin-top:calc(6em - 1vw);
}

在我的 Android 设备上使用 chrome 中的远程调试器,net::ERR_FILE_NOT_FOUND 是我收到的视频文件的唯一问题。由于某种原因正在应用的字体没有这样的错误?

【问题讨论】:

  • 为 webview 启用 javascript 然后重试

标签: android html google-chrome android-webview


【解决方案1】:

我一直将我的文件放在 android 的“assets”文件夹中。然后,您可以对 HTML 执行以下操作:

webView.loadUrl("file:///android_asset/L1.html");

或者,如果您要引用图像或电影文件,我使用了类似的东西:

@SuppressLint({ "SetJavaScriptEnabled", "DefaultLocale" })
private void displayDetail()
{           

        webView.getSettings().setJavaScriptEnabled(true);

        String imageName = animalName.toLowerCase();
        imageName = imageName.replaceAll(" ", "_");
        imageName = imageName.replaceAll("-", "_");
        imageName = imageName.replaceAll("\\(", "");
        imageName = imageName.replaceAll("\\)", "");
        imageName = imageName + ".jpg";

        String html = "<html><body style='font-size: 18px; font-family: \"arial\";'><center><img alt='Embedded Image' src='" + imageName + "'/></center><br><div style='position: relative; margin-left: 10px; margin-right:10px;'>" + breedDetails + "</div></body></html>";

        String mime = "text/html";
        String encoding = "utf-8";

        webView.loadDataWithBaseURL("file:///android_asset/", html, mime, encoding, null);


}   

同时确保您有从外部存储读取的权限。

【讨论】:

  • 抱歉,澄清一下;我将其作为存储在设备上的原始 .html 文件打开,其链接文件也在那里。我没有使用 Android Studio 来构建它,但完全理解这对于最终项目来说更好,哈哈。不过谢谢你的回答!
  • 啊,感谢您的澄清。对于图像,您可以尝试将图像转换为 base64 编码字符串,然后使用 src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/....." 将这些字符串添加到您的 html 中
猜你喜欢
  • 2014-01-19
  • 2014-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-27
  • 1970-01-01
  • 2012-06-21
  • 1970-01-01
相关资源
最近更新 更多