I'm trying to display an image on a browser using NanoHTTPD server
, but always nothing to display.
This is a part of my serve method:
else if(uri.contains(".png")){
SmallBinaryFiles smallBinaryFiles = new SmallBinaryFiles();
InputStream is = new InputStream() {
@Override
public int read() throws IOException {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
};
long i=0;
try {
//smallBinaryFiles.readSmallBinaryFiles(uri): converts binary file given by uri to byte[]
is = new ByteArrayInputStream(smallBinaryFiles.readSmallBinaryFile(uri));
while ((is.read()) != -1){
i++;
}
} catch (IOException ex) {
Logger.getLogger(HelloServer.class.getName()).log(Level.SEVERE, null, ex);
}
return new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, MIME_PNG, is,i);
}
//declaration of MIME_PNG in NanoHTTPD Core
public static final String MIME_PNG = "image/png";
Your while()
loop eats all of your input stream so there's nothing left to send. Put -1
instead of i
to make it a chunk response.
Besides, your read()
method seems to throw an Exception when called. Use FileInputStream
instead.