1

I've been trying to create a server on Android that servers files like MP3. My challenge is that when serving an MP3, I get the File NOT Found Exception

Here's the 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 I start the server after selecting a 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 am I missing?

Relm
  • 7,923
  • 18
  • 66
  • 113

0 Answers0