android - Using the phone's camera -
i m making app uses camera send data surfaceholder . when call addcallback() app crashes. here code :
public class cam_view extends activity implements surfaceholder.callback{ private surfaceview camview; private surfaceholder camholder; private boolean previewrunning; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.camera); camview = (surfaceview)findviewbyid(r.id.sview); camholder = camview.getholder(); camholder.addcallback(this); camholder.settype(surfaceholder.surface_type_push_buffers); }}
and unimplemented methods :
@override public void surfacechanged(surfaceholder holder, int format, int width, int height) { if(previewrunning){ camera.stoppreview(); } camera.parameters camparams = camera.getparameters(); camparams.setpreviewformat(pixelformat.jpeg); camera.setparameters(camparams); try{ camera.setpreviewdisplay(holder); camera.startpreview(); previewrunning=true; }catch(ioexception e){ e.printstacktrace(); } } public void surfacecreated(surfaceholder holder) { try{ camera=camera.open(); }catch(exception e){ e.printstacktrace(); toast.maketext(getapplicationcontext(),"error",toast.length_long).show(); finish(); } } @override public void surfacedestroyed(surfaceholder holder) { camera.stoppreview(); camera.release(); camera=null; }
why app crash when call
camholder.addcallback(this); ? there other problems in code ?
i tried above code.its working fine on device. able launch camera successfully. dont think problem in camholder.addcallback(this);
problem in line
camparams.setpreviewformat(pixelformat.jpeg);
update with
camera.size size = camparams.getsupportedpreviewsizes().get(0); camparams.setpreviewsize(size.width, size.height);
your code work.
also make sure below things added.
1) sview named surfaceview exits in layout
2) add camera permission - "android.permission.camera"
if problem still occurs, please share logs.
Comments
Post a Comment