android - How to send an image to Chromecast using NanoHTTPD -
i want serve file, image example, chromecast. used nanohttpd can reach content via url , here code use url:
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) { file rootdir = environment.getexternalstoragedirectory(); file[] fileslist = null; string filepath = ""; if (uri.trim().isempty()) { fileslist = rootdir.listfiles(); } else { filepath = uri.trim(); } fileslist = new file(filepath).listfiles(); string answer = "<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"><title>sdcard0 - tecno p5 - wifi file transfer pro</title>"; if (new file(filepath).isdirectory()) { (file detailsoffiles : fileslist) { answer += "<a href=\"" + detailsoffiles.getabsolutepath() + "\" alt = \"\">" + detailsoffiles.getabsolutepath() + "</a><br>"; } } else { } answer += "</head></html>" + "uri: " + uri + " \nfiles " + files + " \nparameters " + parameters + " \nheader "; return new nanohttpd.response(answer); } }
this way can url "192.168.x.xx:8080/image.gif". create regular mediainfo url:
private void startvideo() { mediametadata mediametadata = new mediametadata( mediametadata.media_type_photo ); mediametadata.putstring( mediametadata.key_title, getstring( r.string.titulo_video ) ); mediainfo mediainfo = new mediainfo.builder( getstring( r.string.video_url ) ) .setcontenttype( getstring( r.string.content_type_gif ) ) .setstreamtype( mediainfo.stream_type_buffered ) .setmetadata( mediametadata ) .build(); try { mremotemediaplayer.load( apiclient, mediainfo, true ) .setresultcallback( new resultcallback<remotemediaplayer.mediachannelresult>() { @override public void onresult( remotemediaplayer.mediachannelresult mediachannelresult ) { if( mediachannelresult.getstatus().issuccess() ) { mvideoisloaded = true; mbutton.settext( getstring( r.string.pause_video ) ); } } } ); } catch( exception e ) { } }
where <string name="video_url">http://192.168.1.38:8080/sdcard/logo_etsiit.gif</string>
url got before, nothing happens when try cast image.
is possible serve file directly mediainfo without having copy , paste url web browser?
tell me if more code needed.
log errors:
08-07 20:30:35.568 14669-14669/com.example.httpserver w/httpd﹕ web server initialized. 08-07 20:30:35.601 14669-14669/com.example.httpserver i/mediarouter﹕ found default route: mediarouter.routeinfo{ uniqueid=android/.support.v7.media.systemmediarouteprovider:default_route, name=telĂ©fono, description=null, enabled=true, connecting=false, playbacktype=0, playbackstream=3, volumehandling=1, volume=3, volumemax=15, presentationdisplayid=-1, extras=null, providerpackagename=android } 08-07 20:30:35.610 14669-14669/com.example.httpserver w/system.err﹕ java.net.bindexception: bind failed: eaddrinuse (address in use) 08-07 20:30:35.611 14669-14669/com.example.httpserver w/system.err﹕ @ libcore.io.iobridge.bind(iobridge.java:99) 08-07 20:30:35.611 14669-14669/com.example.httpserver w/system.err﹕ @ java.net.plainsocketimpl.bind(plainsocketimpl.java:132) 08-07 20:30:35.611 14669-14669/com.example.httpserver w/system.err﹕ @ java.net.serversocket.bind(serversocket.java:335) 08-07 20:30:35.611 14669-14669/com.example.httpserver w/system.err﹕ @ java.net.serversocket.bind(serversocket.java:295) 08-07 20:30:35.611 14669-14669/com.example.httpserver w/system.err﹕ @ com.example.httpserver.nanohttpd.start(nanohttpd.java:142) 08-07 20:30:35.611 14669-14669/com.example.httpserver w/system.err﹕ @ com.example.httpserver.mainactivity.onresume(mainactivity.java:128) 08-07 20:30:35.611 14669-14669/com.example.httpserver w/system.err﹕ @ android.app.instrumentation.callactivityonresume(instrumentation.java:1257) 08-07 20:30:35.611 14669-14669/com.example.httpserver w/system.err﹕ @ android.app.activity.performresume(activity.java:6076) 08-07 20:30:35.611 14669-14669/com.example.httpserver w/system.err﹕ @ android.app.activitythread.performresumeactivity(activitythread.java:2975) 08-07 20:30:35.611 14669-14669/com.example.httpserver w/system.err﹕ @ android.app.activitythread.handleresumeactivity(activitythread.java:3017) 08-07 20:30:35.611 14669-14669/com.example.httpserver w/system.err﹕ @ android.app.activitythread.handlelaunchactivity(activitythread.java:2392) 08-07 20:30:35.611 14669-14669/com.example.httpserver w/system.err﹕ @ android.app.activitythread.access$800(activitythread.java:151) 08-07 20:30:35.611 14669-14669/com.example.httpserver w/system.err﹕ @ android.app.activitythread$h.handlemessage(activitythread.java:1303) 08-07 20:30:35.611 14669-14669/com.example.httpserver w/system.err﹕ @ android.os.handler.dispatchmessage(handler.java:102) 08-07 20:30:35.611 14669-14669/com.example.httpserver w/system.err﹕ @ android.os.looper.loop(looper.java:135) 08-07 20:30:35.611 14669-14669/com.example.httpserver w/system.err﹕ @ android.app.activitythread.main(activitythread.java:5254) 08-07 20:30:35.611 14669-14669/com.example.httpserver w/system.err﹕ @ java.lang.reflect.method.invoke(native method) 08-07 20:30:35.612 14669-14669/com.example.httpserver w/system.err﹕ @ java.lang.reflect.method.invoke(method.java:372) 08-07 20:30:35.612 14669-14669/com.example.httpserver w/system.err﹕ @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) 08-07 20:30:35.612 14669-14669/com.example.httpserver w/system.err﹕ @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698) 08-07 20:30:35.612 14669-14669/com.example.httpserver w/system.err﹕ caused by: android.system.errnoexception: bind failed: eaddrinuse (address in use) 08-07 20:30:35.612 14669-14669/com.example.httpserver w/system.err﹕ @ libcore.io.posix.bind(native method) 08-07 20:30:35.612 14669-14669/com.example.httpserver w/system.err﹕ @ libcore.io.forwardingos.bind(forwardingos.java:56) 08-07 20:30:35.612 14669-14669/com.example.httpserver w/system.err﹕ @ libcore.io.iobridge.bind(iobridge.java:97) 08-07 20:30:35.612 14669-14669/com.example.httpserver w/system.err﹕ ... 19 more
when select image web browser can see is:
uri: /sdcard/logo_etsiit.gif files {} parameters {} header
edit: solved problem webserver class, can see image or pdf selected in web browser
public class webserver extends nanohttpd { fileinputstream fileinputstream; 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 mediasend=getextension(chosenfile); fileinputstream fis = null; try { fis = new fileinputstream(environment.getexternalstoragedirectory() + "/" + chosenfile); } catch (filenotfoundexception e) { e.printstacktrace(); } return new nanohttpd.response(etsiit.etsiitcast_def.nanohttpd.response.status.ok,mediasend,fis); } }
i added code oncreate:
//get ip of device running nanohttpd server wifimanager wifimanager = (wifimanager) getsystemservice(wifi_service); wifiinfo wifiinfo = wifimanager.getconnectioninfo(); int ipaddress = wifiinfo.getipaddress(); ipdevice=string.format("http://%d.%d.%d.%d:8080",(ipaddress & 0xff),(ipaddress >> 8 & 0xff),(ipaddress >> 16 & 0xff),(ipaddress >> 24 & 0xff)); // start webserver mediaserver = new webserver(); try { mediaserver.start(); } catch(ioexception ioe) { log.d("httpd", "the server not start."); }
i found solution problem: had change mediainfo mediainfo = new mediainfo.builder( getstring( r.string.video_url ) )
to mediainfo mediainfo = new mediainfo.builder(ipdevice)
ipdevice
phone ip address (e.g. http://192.168.1.40:8080).
Comments
Post a Comment