我需要創建一個本地服務器,它能夠爲設備的本地文件提供服務。我發現這library,它似乎能夠滿足我的需求。使用NanoHttpd在本地服務器上提供圖像
我已經嘗試過這個項目中的示例,它工作正常!但它張貼.html頁面,我需要圖片。
我跟着這個post其中.mp3被送達。但是這對我不起作用。可能是因爲圖書館從那時起更新了。
@Override
public Response serve(IHTTPSession session) {
FileInputStream fis = null;
try {
File file = new File(Environment.getExternalStoragePublicDirectory(ROOT)
+ PATH + "picture.jpg"); //path exists and its correct
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new NanoHTTPD.Response(Response.Status.OK, "image/jpeg", fis, 1000000); //the last parameter is totalBytes. Not sure what to put there
}
服務器中被啓動的onCreate:
//start web server
try {
server = new WebServer();
server.start();
} catch (IOException e) {
e.printStackTrace();
}
目的是訪問這樣的文件:192.168.1.2:8080/picture.jpg
任何人都可以提出解決方案?提前致謝。
開始。一個與uri和參數。就像您提供的那篇文章的鏈接一樣。然後你可以提取'picture.jpg。第一個或其他文件名。而你沒有告訴你什麼不適合你。 '提前致謝.'非常錯誤。如果你得到了很好的幫助,你應該承諾感謝。 – greenapps
@greenapps,帶參數的'serve()'已棄用。無論如何,我已經嘗試過了。他們中的任何一個都不像提供文件。我無法通過ip訪問它們。屏幕上沒有顯示任何內容 – AnZ
您的代碼應該沒問題,但要刪除一個參數:'1000000); //最後一個參數是totalBytes。' – greenapps