我正在尝试在Chrome自定义标签中的资源文件夹中加载文件。为此,我正在使用NanoHTTPD创建一个HTTP服务器。但是,我无法加载资源文件夹中的文件。这是MainActivity.java
package com.example.webar;
import ...
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button loadbtn = findViewById(R.id.loadbtn);
final int port = 8000;
final Server server = new Server(port);
loadbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
server.start();
final CustomTabsIntent ctab = new CustomTabsIntent.Builder().build();
String url = "http:/localhost:"+server.getListeningPort()+"/";
System.out.println("-------"+server.getHostname());
ctab.launchUrl(MainActivity.this, Uri.parse(url));
}
catch (IOException ioe){
System.out.println("-------Server didn't start");
server.stop();
}
}
});
}}
class Server extends NanoHTTPD {
public Server(int port) {
super(port);
}
@Override
public Response serve(IHTTPSession session) {
String uri = session.getUri();
if (uri.equals("/")) {
try {
String path = "assets/index.html";
File file = new File(path);
System.out.println("---------"+file.exists());
FileInputStream index = new FileInputStream(path);
return newChunkedResponse(Response.Status.OK,MIME_HTML, index);
}
catch (FileNotFoundException fe){
System.out.println("------File Not found");
}
}
return null;
}
}
我已经尝试了路径file:///android_assets/index.html(可用于网络视图),但是在这里不起作用。
答案 0 :(得分:0)
我使用InputStream。
InputStream is = context.getAssets().open("login.htm");
然后在我拥有的src / main / assets中
/css
/fonts
/img
/js
login.htm
logout.htm