2016-09-08 71 views
0

我試圖在瀏覽器上使用NanoHTTPD server顯示圖像,但始終沒有顯示。 這是我的發球方法的一部分:NanoHTTPD:顯示圖像

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"; 

回答

1

while()循環吃所有的輸入流中的所以沒有什麼留下來發送。把-1而不是i作爲一個塊響應。

此外,您的read()方法似乎在調用時會拋出異常。改爲使用FileInputStream