我想投射从图库中选择的本地数据(图像/视频),并使用NanoHTTPD服务器在本地Web服务器上提供。 提供图像的代码是:
public class MyServer extends NanoHTTPD {
FileInputStream fileInputStream;
private final static int PORT = 8888;
public MyServer() throws IOException {
super(PORT);
start();
System.out.println("\nRunning! Point your browers to http://localhost:8888/ \n");
}
public Response serve(String uri, Method method, Map<String, String> header, Map<String, String> parameters, Map<String, String> files) {
String mediasend = " ";
FileInputStream fis = null;
String file;
String answer = "";
try {
fis = new FileInputStream( selectedImagePath );
mediasend= selectedImagePath.substring(selectedImagePath.lastIndexOf(".")+1);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return new NanoHTTPD.Response(com.madhu.imageonserver.Server.NanoHTTPD.Response.Status.OK, mediasend, fis);
}
}
其中selectedImagePath是:“/ storage / sdcard0 / whatsApp / WhatsApp Images / IMG-20160409-WA000.jpg”。 mediaInfo用于投射图像。
mediaMetadata.putString(com.google.android.gms.cast.MediaMetadata.KEY_TITLE, getString(R.string.image_title));
MediaInfo mediaInfo = new MediaInfo.Builder(ipdevice)
.setContentType(getString(R.string.content_type_jpg))
.setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
.setMetadata(mediaMetadata)
.build();
其中mediaInfo:MediaInfo @ 830029452976 ipdevice:“here”。
服务器在onCreate中启动:
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
ipdevice = String.format("http://%d.%d.%d.%d:8888", (ipAddress & 0xff), (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));
try {
server.start();
} catch (IOException ioe) {
Log.d("Httpd", "The server could not start.");
}
但是当我尝试投射图像时没有任何反应。 目的是投射图像和视频等本地数据。
有人能建议我更好的解决方案吗?