How to release camera when camera is opened?

How to release camera when camera is opened?



My app controls the camera and recently there have been a lot of crash reports by devices that cannot open the camera. Pressing the back button releases the camera but also disables the background mode. Pressing the home button doesn't release the camera, causes a crash when opening camera, and yet enables background mode.



How can I release the camera when the camera app itself is opened?




2 Answers
2



When you use android hardware camera in your android application you should release it so other application or your application can use it.
It is good practice to release camera before moving to another activity in your app.



Use below code -


protected void onDestroy()

if(camera!=null)
camera.stopPreview();
camera.setPreviewCallback(null);

camera.release();
camera = null;




above code will release hardware camera,which you was open using Camera.open(); method.


Camera.open()



and if you have your custom preview then you have to use in side your surfaceDestroyed method -


surfaceDestroyed


public void surfaceDestroyed(SurfaceHolder holder)

if(camera!=null)
camera.stopPreview();
camera.setPreviewCallback(null);

camera.release();
camera = null;




Hope it will help you.





This could benefit from some comments, or explanation. Also, there are some typos (onDestory).
– Patrick McLaren
Apr 29 '15 at 4:33



onDestory





Doesn't answer the question. It needs to be only when the camera app is opened.
– Skyfall
Apr 29 '15 at 4:34






so inside your activity onDestroy method put my code.And if you want to close camera after image is captured then you can use same code there.
– Ravi Bhandari
Apr 29 '15 at 4:43





You still do not understand Ravi. Is it possible to stop the app completely when the camera is opened?
– Skyfall
Apr 29 '15 at 4:50





Ok, in that case check this - stackoverflow.com/questions/6330200/… answer
– Ravi Bhandari
Apr 29 '15 at 4:53



It is considered safe practice to release camera when onPause() of the relevant Activity is invoked. In line with that, I'd encourage not to pass active Camera object from one Activity to another. I often use Fragments to change the UI while still keeping the Camera active.


onPause()



There are some special cases, when the app must use the camera even after onPause(), see e.g. Detect when another app tries to use the camera. Not in all cases this is possible, but never it is easy to handle.



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế