java - takePicture in service not working -


i'm trying take picture camera , it's bytes in serice using code:

camera camera = camera.open(); surfaceview view = new surfaceview(getapplicationcontext()); surfaceholder holder = view.getholder(); camera.getparameters().setpreviewsize(1, 1); camera.setpreviewdisplay(holder); camera.startpreview(); camera.takepicture(null, picturecallback, null); 

but not working. not getting exception picturecallback never being called.

taken android docs: http://developer.android.com/training/camera/photobasics.html

you should call camera intent this:

first add necessary permissions app in androidmanifest file:

<manifest ... >     <uses-feature android:name="android.hardware.camera"                   android:required="true" />     ... </manifest> 

after call corresponding intent start camera:

static final int request_image_capture = 1;  private void dispatchtakepictureintent() {     intent takepictureintent = new intent(mediastore.action_image_capture);     if (takepictureintent.resolveactivity(getpackagemanager()) != null) {         startactivityforresult(takepictureintent, request_image_capture);     } } 

the data receive camera called in following method, able store or use wish:

@override protected void onactivityresult(int requestcode, int resultcode, intent data) {     if (requestcode == request_image_capture && resultcode == result_ok) {         bundle extras = data.getextras();         bitmap imagebitmap = (bitmap) extras.get("data");         mimageview.setimagebitmap(imagebitmap);     } } 

in case want implement in service, taking code link, should able take picture service:

mpreview = new camerapreview(this, mcamera, jpegcallback); windowmanager wm = (windowmanager)         .getsystemservice(context.window_service); windowmanager.layoutparams params = new windowmanager.layoutparams(         windowmanager.layoutparams.wrap_content,         windowmanager.layoutparams.wrap_content,         windowmanager.layoutparams.type_system_overlay,         windowmanager.layoutparams.flag_watch_outside_touch,         pixelformat.transparent);  params.height = 1; params.width = 1;  wm.addview(mpreview, params); 

as stated comments, please note needs permission system_alert_window work, users might not want allow app use permission.


Comments