How to Fix unplug usb cable error in android with usb web cam? -
i created android application based on connecting webcam. application works charm while connection webcam. when unplug, mobile application crashed , shows "unfortunately application stopped working".
video activity:
public class videoactivity extends appcompatactivity { actionbar actionbar; public static int mcurrentposition = -1; private handler handler; private runnable mrunnable; //-------------------------------- private static final boolean debug = true; private static final string tag = "video activity"; private static final int default_width = 640; //640 private static final int default_height = 480; //480 private usbmonitor musbmonitor; private icameraclient mcameraclient; private cameraviewinterface mcameraview; private boolean issubview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.new_video_layout); if (musbmonitor == null) { musbmonitor = new usbmonitor(getapplicationcontext(), mondeviceconnectlistener); final list<devicefilter> filters = devicefilter.getdevicefilters(getapplicationcontext(), r.xml.device_filter); musbmonitor.setdevicefilter(filters); } if (savedinstancestate != null) { mcurrentposition = savedinstancestate.getint("state"); } actionbar = getsupportactionbar(); assert actionbar != null; actionbar.setdisplayshowcustomenabled(true); actionbar.setbackgrounddrawable(new colordrawable(color.white)); actionbar.setdisplayshowtitleenabled(false); actionbar.setdisplayshowhomeenabled(false); layoutinflater inflator = (layoutinflater) this.getsystemservice(context.layout_inflater_service); view v = inflator.inflate(r.layout.custom_actionbar, null); actionbar.setcustomview(v); mcameraview = (cameraviewinterface) findviewbyid(r.id.camera_view); mcameraview.setaspectratio(default_width / (float) default_height); mcameraview.setcallback(mcallback); } @override public void onbackpressed() { finish(); movetasktoback(true); super.onbackpressed(); } private final usbmonitor.ondeviceconnectlistener mondeviceconnectlistener = new usbmonitor.ondeviceconnectlistener() { @override public void onattach(final usbdevice device) { if (debug) log.v(tag, "ondeviceconnectlistener#onattach:"); if (!updatecameradialog() && (mcameraview.getsurface() != null)) { tryopenuvccamera(true); } } @override public void onconnect(final usbdevice device, final usbmonitor.usbcontrolblock ctrlblock, final boolean createnew) { if (debug) log.v(tag, "ondeviceconnectlistener#onconnect:"); } @override public void ondisconnect(final usbdevice device, final usbmonitor.usbcontrolblock ctrlblock) { if (debug) log.v(tag, "ondeviceconnectlistener#ondisconnect:"); } @override public void ondettach(final usbdevice device) { if (debug) log.v(tag, "ondeviceconnectlistener#ondettach:"); if (mcameraclient != null) { mcameraclient.disconnect(); mcameraclient.release(); mcameraclient = null; } updatecameradialog(); } @override public void oncancel() { if (debug) log.v(tag, "ondeviceconnectlistener#oncancel:"); } }; private boolean updatecameradialog() { final fragment fragment = getfragmentmanager().findfragmentbytag("cameradialog"); if (fragment instanceof cameradialog) { ((cameradialog) fragment).updatedevices(); return true; } return false; } private void tryopenuvccamera(final boolean requestpermission) { if (debug) log.v(tag, "tryopenuvccamera:"); openuvccamera(0); } public void openuvccamera(final int index) { if (debug) log.v(tag, "openuvccamera:index=" + index); if (!musbmonitor.isregistered()) return; final list<usbdevice> list = musbmonitor.getdevicelist(); if (list.size() > index) { if (mcameraclient == null) mcameraclient = new cameraclient(getapplicationcontext(), mcameralistener); mcameraclient.select(list.get(index)); mcameraclient.resize(default_width, default_height); mcameraclient.connect(); } } private final cameraviewinterface.callback mcallback = new cameraviewinterface.callback() { @override public void onsurfacecreated(final surface surface) { tryopenuvccamera(true); } @override public void onsurfacechanged(final surface surface, final int width, final int height) { } @override public void onsurfacedestroy(final surface surface) { } }; private final icameraclientcallback mcameralistener = new icameraclientcallback() { @override public void onconnect() { if (debug) log.v(tag, "onconnect:"); mcameraclient.addsurface(mcameraview.getsurface(), false); issubview = true; } @override public void ondisconnect() { if (debug) log.v(tag, "ondisconnect:"); } };}
uvccamera:
public class uvcservice extends service { private static final boolean debug = true; private static final boolean checkconn = true; private static final string tag = "uvcservice"; private usbmonitor musbmonitor; public uvcservice() { if (debug) log.d(tag, "constructor:"); } @override public void oncreate() { super.oncreate(); if (debug) log.d(tag, "oncreate:"); if (musbmonitor == null) { musbmonitor = new usbmonitor(getapplicationcontext(), mondeviceconnectlistener); musbmonitor.register(); } } @override public void ondestroy() { if (debug) log.d(tag, "ondestroy:"); if (checkreleaseservice()) { if (musbmonitor != null) { musbmonitor.unregister(); musbmonitor = null; } } super.ondestroy(); } @override public ibinder onbind(final intent intent) { if (debug) log.d(tag, "onbind:" + intent); if (iuvcservice.class.getname().equals(intent.getaction())) { log.i(tag, "return mbasicbinder"); return mbasicbinder; } if (iuvcslaveservice.class.getname().equals(intent.getaction())) { log.i(tag, "return mslavebinder"); return mslavebinder; } return null; } @override public void onrebind(final intent intent) { if (debug) log.d(tag, "onrebind:" + intent); } @override public boolean onunbind(final intent intent) { if (debug) log.d(tag, "onunbind:" + intent); if (checkreleaseservice()) { musbmonitor.unregister(); musbmonitor = null; } return true; } //******************************************************************************** private final ondeviceconnectlistener mondeviceconnectlistener = new ondeviceconnectlistener() { @override public void onattach(final usbdevice device) { if (debug) log.d(tag, "ondeviceconnectlistener#onattach:"); } @override public void onconnect(final usbdevice device, final usbcontrolblock ctrlblock, final boolean createnew) { if (debug) log.d(tag, "ondeviceconnectlistener#onconnect:"); final int key = device.hashcode(); cameraserver service; synchronized (sservicesync) { service = scameraservers.get(key); if (service == null) { service = cameraserver.createserver(uvcservice.this, ctrlblock, device.getvendorid(), device.getproductid()); scameraservers.append(key, service); } else { log.w(tag, "service exist before connection"); } sservicesync.notifyall(); } } @override public void ondisconnect(final usbdevice device, final usbcontrolblock ctrlblock) { if (debug) log.d(tag, "ondeviceconnectlistener#ondisconnect:"); if (!checkconn) { processstopservice(uvcservice.tag); } removeservice(device); /*if(!checkconn){this.stopservice(new intent(this, myservice.class));} ;*/ } @override public void ondettach(final usbdevice device) { if (debug) log.d(tag, "ondeviceconnectlistener#ondettach:"); if (!checkconn) { processstopservice(uvcservice.tag); } removeservice(device); } @override public void oncancel() { if (debug) log.d(tag, "ondeviceconnectlistener#oncancel:"); synchronized (sservicesync) { sservicesync.notifyall(); } } }; // ====================================================================================== private void processstopservice(final string tag) { intent intent = new intent(getapplicationcontext(), uvcservice.class); intent.addcategory(tag); stopservice(intent); } // ====================================================================================== private void removeservice(final usbdevice device) { final int key = device.hashcode(); synchronized (sservicesync) { final cameraserver service = scameraservers.get(key); if (service != null) service.release(); scameraservers.remove(key); sservicesync.notifyall(); } if (checkreleaseservice()) { if (musbmonitor != null) { musbmonitor.unregister(); musbmonitor = null; } } } //******************************************************************************** private static final object sservicesync = new object(); private static final sparsearray<cameraserver> scameraservers = new sparsearray<cameraserver>(); /** * cameraservice has specific id<br> * if 0 provided id, return top of cameraserver instance(non-blocking method) if exists or null.<br> * if non-zero id provided, return specific cameraservice if exist. block if not exists.<br> * return null if not exist matched specific id<br> * * @param serviceid * @return */ private static cameraserver getcameraserver(final int serviceid) { synchronized (sservicesync) { cameraserver server = null; if ((serviceid == 0) && (scameraservers.size() > 0)) { server = scameraservers.valueat(0); } else { server = scameraservers.get(serviceid); if (server == null) try { log.i(tag, "waitting service ready"); sservicesync.wait(); } catch (final interruptedexception e) { } server = scameraservers.get(serviceid); } return server; } } /** * @return true if there no camera connection */ private static boolean checkreleaseservice() { cameraserver server = null; synchronized (sservicesync) { final int n = scameraservers.size(); if (debug) log.d(tag, "checkreleaseservice:number of service=" + n); (int = 0; < n; i++) { server = scameraservers.valueat(i); log.i(tag, "checkreleaseservice:server=" + server + ",isconnected=" + (server != null ? server.isconnected() : false)); if (server != null && !server.isconnected()) { scameraservers.removeat(i); server.release(); } } return scameraservers.size() == 0; } } //******************************************************************************** private final iuvcservice.stub mbasicbinder = new iuvcservice.stub() { private iuvcservicecallback mcallback; @override public int select(final usbdevice device, final iuvcservicecallback callback) throws remoteexception { if (debug) log.d(tag, "mbasicbinder#select:device=" + (device != null ? device.getdevicename() : null)); mcallback = callback; final int serviceid = device.hashcode(); cameraserver server = null; synchronized (sservicesync) { server = scameraservers.get(serviceid); if (server == null) { log.i(tag, "request permission"); musbmonitor.requestpermission(device); log.i(tag, "wait getting permission"); try { sservicesync.wait(); } catch (final exception e) { log.e(tag, "connect:", e); } log.i(tag, "check service again"); server = scameraservers.get(serviceid); if (server == null) { throw new runtimeexception("failed open usb device(has no permission)"); } } } if (server != null) { log.i(tag, "success service:serviceid=" + serviceid); server.registercallback(callback); } return serviceid; } @override public void release(final int serviceid) throws remoteexception { if (debug) log.d(tag, "mbasicbinder#release:"); synchronized (sservicesync) { final cameraserver server = scameraservers.get(serviceid); if (server != null) { if (server.unregistercallback(mcallback)) { if (!server.isconnected()) { scameraservers.remove(serviceid); if (server != null) { server.release(); } final cameraserver srv = scameraservers.get(serviceid); log.w(tag, "srv=" + srv); } } } } mcallback = null; } @override public boolean isselected(final int serviceid) throws remoteexception { return getcameraserver(serviceid) != null; } @override public void releaseall() throws remoteexception { if (debug) log.d(tag, "mbasicbinder#releaseall:"); cameraserver server; synchronized (sservicesync) { final int n = scameraservers.size(); (int = 0; < n; i++) { server = scameraservers.valueat(i); scameraservers.removeat(i); if (server != null) { server.release(); } } } } @override public void resize(final int serviceid, final int width, final int height) { if (debug) log.d(tag, "mbasicbinder#resize:"); final cameraserver server = getcameraserver(serviceid); if (server == null) { throw new illegalargumentexception("invalid serviceid"); } server.resize(width, height); } @override public void connect(final int serviceid) throws remoteexception { if (debug) log.d(tag, "mbasicbinder#connect:"); final cameraserver server = getcameraserver(serviceid); if (server == null) { throw new illegalargumentexception("invalid serviceid"); } server.connect(); } @override public void disconnect(final int serviceid) throws remoteexception { if (debug) log.d(tag, "mbasicbinder#disconnect:"); final cameraserver server = getcameraserver(serviceid); if (server == null) { throw new illegalargumentexception("invalid serviceid"); } server.disconnect(); } @override public boolean isconnected(final int serviceid) throws remoteexception { final cameraserver server = getcameraserver(serviceid); return (server != null) && server.isconnected(); } @override public void addsurface(final int serviceid, final int id_surface, final surface surface, final boolean isrecordable) throws remoteexception { if (debug) log.d(tag, "mbasicbinder#addsurface:id=" + id_surface + ",surface=" + surface); final cameraserver server = getcameraserver(serviceid); if (server != null) server.addsurface(id_surface, surface, isrecordable, null); } @override public void removesurface(final int serviceid, final int id_surface) throws remoteexception { if (debug) log.d(tag, "mbasicbinder#removesurface:id=" + id_surface); final cameraserver server = getcameraserver(serviceid); if (server != null) server.removesurface(id_surface); } @override public boolean isrecording(final int serviceid) throws remoteexception { final cameraserver server = getcameraserver(serviceid); return server != null && server.isrecording(); } @override public void startrecording(final int serviceid) throws remoteexception { if (debug) log.d(tag, "mbasicbinder#startrecording:"); final cameraserver server = getcameraserver(serviceid); if ((server != null) && !server.isrecording()) { server.startrecording(); } } @override public void stoprecording(final int serviceid) throws remoteexception { if (debug) log.d(tag, "mbasicbinder#stoprecording:"); final cameraserver server = getcameraserver(serviceid); if ((server != null) && server.isrecording()) { server.stoprecording(); } } @override public void capturestillimage(final int serviceid, final string path) throws remoteexception { if (debug) log.d(tag, "mbasicbinder#capturestillimage:" + path); final cameraserver server = getcameraserver(serviceid); if (server != null) { server.capturestill(path); } } }; //******************************************************************************** private final iuvcslaveservice.stub mslavebinder = new iuvcslaveservice.stub() { @override public boolean isselected(final int serviceid) throws remoteexception { return getcameraserver(serviceid) != null; } @override public boolean isconnected(final int serviceid) throws remoteexception { final cameraserver server = getcameraserver(serviceid); return server != null ? server.isconnected() : false; } @override public void addsurface(final int serviceid, final int id_surface, final surface surface, final boolean isrecordable, final iuvcserviceonframeavailable callback) throws remoteexception { if (debug) log.d(tag, "mslavebinder#addsurface:id=" + id_surface + ",surface=" + surface); final cameraserver server = getcameraserver(serviceid); if (server != null) { server.addsurface(id_surface, surface, isrecordable, callback); } else { log.e(tag, "failed cameraserver:serviceid=" + serviceid); } } @override public void removesurface(final int serviceid, final int id_surface) throws remoteexception { if (debug) log.d(tag, "mslavebinder#removesurface:id=" + id_surface); final cameraserver server = getcameraserver(serviceid); if (server != null) { server.removesurface(id_surface); } else { log.e(tag, "failed cameraserver:serviceid=" + serviceid); } } };
}
you should use try catch statements make sure program doesn't crash because of uncaught errors:
@override public void ondettach(final usbdevice device) { if (debug) log.v(tag, "ondeviceconnectlistener#ondettach:"); try { if (mcameraclient != null) { mcameraclient.disconnect(); mcameraclient.release(); mcameraclient = null; } updatecameradialog(); } catch (exception ex) { log.e("exception occurred", ex); } }
normally catching exceptions bad practice, should check exception thrown , change exception catch specific exception.
Comments
Post a Comment