java - NanoHttpd : File Not Found when trying to serve a file -
i've been trying create server on android servers files mp3. challenge when serving mp3, file not found exception
here's server class below.
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"); } }
and here's how start server after selecting file.
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(); } } } }
what missing?
Comments
Post a Comment