我一直在尝试在Android上创建一个服务器文件,如MP3。
我的挑战是,当提供MP3时,我得到File NOT Found Exception
以下是服务器类。
public class Server extends NanoHTTPD {
private static final String MIME_AUDIO = "audio/mpeg";
private String URI;
public Server(String uri,String type) throws IOException {
super(8081);
this.URI = uri;
start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
}
@Override
public Response serve(IHTTPSession session) {
FileInputStream fis = null;
try {
fis = new FileInputStream(Environment.getExternalStorageDirectory() + "/"+URI);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
int bytes = 0;
if(fis != null){
bytes = fis.available();
}
return newFixedLengthResponse(Response.Status.OK, MIME_AUDIO, fis, bytes);
} catch (IOException e) {
e.printStackTrace();
}
return newFixedLengthResponse(Response.Status.OK, MIME_HTML, "Unknown Request");
}
}
以下是选择文件后启动服务器的方法。
public void onActivityResult(int i, int resultCode, Intent intent) {
super.onActivityResult(i, resultCode, intent);
switch (i) {
case 1013:
if (resultCode == RESULT_OK) {
Uri uri = intent.getData();
try {
new Server(uri.toString(),"audio").start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
我错过了什么?