Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve a file on sdcard using NanoHTTPD (inside Android)

I've written a small Android server using NanoHTTPD. It can serve an HTML file well (web page located at sdcard/www/index.html). Can anybody please help me find out how can I serve an audio or video file instead of an html page using NanoHTTPD? Forgive me if the question seems silly, as I'm new to HTTP! Here is my server side code (I've replaced the webpage path to that of an audio file):

package com.example.zserver;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

public class MainActivity extends Activity {

    private WebServer server;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        server = new WebServer();
        try {
            server.start();
        } catch(IOException ioe) {
            Log.w("Httpd", "The server could not start.");
        }
        Log.w("Httpd", "Web server initialized.");

    }
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        if (server != null)
            server.stop();
    }

    private class WebServer extends NanoHTTPD {

        public WebServer()
        {
            super(8080);
        }

        @Override
        public Response serve(String uri, Method method, 
                              Map<String, String> header,
                              Map<String, String> parameters,
                              Map<String, String> files) {
            String answer = "";
            try {
                // Opening file from SD Card
                File root = Environment.getExternalStorageDirectory();
                FileReader index = new FileReader(root.getAbsolutePath() +
                        "/www/music.mp3");              
                BufferedReader reader = new BufferedReader(index);
                String line = "";
                while ((line = reader.readLine()) != null) {
                    answer += line;
                }

            } catch(IOException ioe) {
                Log.w("Httpd", ioe.toString());
            }


            return new NanoHTTPD.Response(answer);
        }
    }

}

I've included necessary use-permissions in my code:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Any help would be appreciated. Thanks in advance!

EDIT, Added permission:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

EDIT1: Canonical way of reading and writing to buffers:

int read;
        int n=1;
        while((read = dis.read(mybytearray)) != -1){
            dos.write(mybytearray, 0, read);}
like image 454
Vinit Shandilya Avatar asked Oct 14 '13 11:10

Vinit Shandilya


1 Answers

Firstly, you need to make sure you set the mimetype properly when serving media files.

Secondly, you won't get very far reading a MP3 file line by line using FileReader, instead you should provide NanoHTTPD with an InputStream.

Below is a working modified version of your code, which serves a MP3 file. By setting the mimetype to audio/mpeg you let the browser decide what to do with this content. In Chrome, for example, the integrated music player is launched and plays the file.

public class StackOverflowMp3Server extends NanoHTTPD {

    public StackOverflowMp3Server() {
         super(8089);
    }

    @Override
    public Response serve(String uri, Method method,
        Map<String, String> header, Map<String, String> parameters,
        Map<String, String> files) {
    String answer = "";

    FileInputStream fis = null;
    try {
        fis = new FileInputStream(Environment.getExternalStorageDirectory()
                + "/music/musicfile.mp3");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return new NanoHTTPD.Response(Status.OK, "audio/mpeg", fis);
  }
}

EDIT: a lot of people have been asking how to make the audio file seekable using range requests, I'll demonstrate this below

To make the audio file seekable, range requests are used which enable HTTP clients to retrieve parts of the audio file in chunks. Make sure that you're serving the file with the PARTIAL_CONTENT response status (HTTP 206). An example implementation can be found in the example code of NanoHTTPD: SimpleWebserver.java

In my implementation, instead of returning a NanoHTTPD Response directly in the serve method, I create another method called "servefile" which I use as the response for handling the range requests, as you can see below. This code is a modified implementation of the SimpleWebServer.java I linked above.

  @Override
  public Response serve(String uri, Method method,
        Map<String, String> header, Map<String, String> parameters,
        Map<String, String> files) {

    File f = new File(Environment.getExternalStorageDirectory()
            + "/music/musicfile.mp3");      
    String mimeType =  "audio/mpeg";

    return serveFile(uri, header, f, mimeType);
}
//Announce that the file server accepts partial content requests
private Response createResponse(Response.Status status, String mimeType,
        InputStream message) {
    Response res = new Response(status, mimeType, message);
    res.addHeader("Accept-Ranges", "bytes");
    return res;
}

/**
 * Serves file from homeDir and its' subdirectories (only). Uses only URI,
 * ignores all headers and HTTP parameters.
 */
private Response serveFile(String uri, Map<String, String> header,
        File file, String mime) {
    Response res;
    try {
        // Calculate etag
        String etag = Integer.toHexString((file.getAbsolutePath()
                + file.lastModified() + "" + file.length()).hashCode());

        // Support (simple) skipping:
        long startFrom = 0;
        long endAt = -1;
        String range = header.get("range");
        if (range != null) {
            if (range.startsWith("bytes=")) {
                range = range.substring("bytes=".length());
                int minus = range.indexOf('-');
                try {
                    if (minus > 0) {
                        startFrom = Long.parseLong(range
                                .substring(0, minus));
                        endAt = Long.parseLong(range.substring(minus + 1));
                    }
                } catch (NumberFormatException ignored) {
                }
            }
        }

        // Change return code and add Content-Range header when skipping is
        // requested
        long fileLen = file.length();
        if (range != null && startFrom >= 0) {
            if (startFrom >= fileLen) {
                res = createResponse(Response.Status.RANGE_NOT_SATISFIABLE,
                        NanoHTTPD.MIME_PLAINTEXT, "");
                res.addHeader("Content-Range", "bytes 0-0/" + fileLen);
                res.addHeader("ETag", etag);
            } else {
                if (endAt < 0) {
                    endAt = fileLen - 1;
                }
                long newLen = endAt - startFrom + 1;
                if (newLen < 0) {
                    newLen = 0;
                }

                final long dataLen = newLen;
                FileInputStream fis = new FileInputStream(file) {
                    @Override
                    public int available() throws IOException {
                        return (int) dataLen;
                    }
                };
                fis.skip(startFrom);

                res = createResponse(Response.Status.PARTIAL_CONTENT, mime,
                        fis);
                res.addHeader("Content-Length", "" + dataLen);
                res.addHeader("Content-Range", "bytes " + startFrom + "-"
                        + endAt + "/" + fileLen);
                res.addHeader("ETag", etag);
            }
        } else {
            if (etag.equals(header.get("if-none-match")))
                res = createResponse(Response.Status.NOT_MODIFIED, mime, "");
            else {
                res = createResponse(Response.Status.OK, mime,
                        new FileInputStream(file));
                res.addHeader("Content-Length", "" + fileLen);
                res.addHeader("ETag", etag);
            }
        }
    } catch (IOException ioe) {
        res = createResponse(Response.Status.FORBIDDEN,
                NanoHTTPD.MIME_PLAINTEXT, "FORBIDDEN: Reading file failed.");
    }

    return res;
}
like image 198
Zebaz Avatar answered Nov 08 '22 11:11

Zebaz