Taking photo in android -


hi developing app camera functionality surfaceview. want take picture when surface view created.

in fragment's oncreateview method, initialized need.

@override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         // inflate layout fragment         view = inflater.inflate(r.layout.game_one_start_fragment, container, false);          initwidgets(view);             return view;      } 

in initwidgets() method initialized surfaceview object below

msurfaceview = (surfaceview) view.findviewbyid(r.id.game_action_surfaceview); msurfaceholder = msurfaceview.getholder(); 

in onresume() method getting surfaceholder object.

@override     public void onresume() {         super.onresume();         msurfaceholder.addcallback(this);         msurfaceholder.settype(surfaceholder.surface_type_push_buffers);         safecameraopen(camera.camerainfo.camera_facing_back);      } 

safecameraopen() have followed android developer link. used same code mentioned.

the surfaceholder.callback used below

@override     public void surfacecreated(surfaceholder holder) {          mcamera.startpreview();      }       @override     public void surfacechanged(surfaceholder holder, int format, int width, int height) {          try { //to set picture orientation             setcameradisplayorientation(getactivity(), findfrontfacingcameraid(), mcamera);             mcamera.startpreview();             mcamera.setpreviewdisplay(holder);          } catch (ioexception e) {              e.printstacktrace();          }          camera.parameters parameters = mcamera.getparameters();         parameters.getsupportedpreviewsizes();          mcamera.setparameters(parameters);       }      @override     public void surfacedestroyed(surfaceholder holder) {         mcamera.stoppreview();         mcamera.release();     } 

i want take picture when fragment loaded.

i have tried in onviewcreated(),onresume() , onstart() lifecycle methods. within them have used following line

 mcamera.takepicture(null,null,mpicturecallback); 

(mcamera object created , initialized in safecameraopen() method)

if tried within mentioned lifecycle methods, getting following crash error.

java.lang.runtimeexception: takepicture failed             @ android.hardware.camera.native_takepicture(native method)             @ android.hardware.camera.takepicture(camera.java:1266)             @ android.hardware.camera.takepicture(camera.java:1211)             @ com.gaming.fragment.gameonestartfragment.snappicture(gameonestartfragment.java:76)             @ com.gaming.fragment.gameonestartfragment.surfacecreated(gameonestartfragment.java:284)             @ android.view.surfaceview.updatewindow(surfaceview.java:572)             @ android.view.surfaceview.access$000(surfaceview.java:86)             @ android.view.surfaceview$3.onpredraw(surfaceview.java:175)             @ android.view.viewtreeobserver.dispatchonpredraw(viewtreeobserver.java:847)             @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1897)             @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1019)             @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:5725)             @ android.view.choreographer$callbackrecord.run(choreographer.java:761)             @ android.view.choreographer.docallbacks(choreographer.java:574)             @ android.view.choreographer.doframe(choreographer.java:544)             @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:747)             @ android.os.handler.handlecallback(handler.java:733)             @ android.os.handler.dispatchmessage(handler.java:95)             @ android.os.looper.loop(looper.java:136)             @ android.app.activitythread.main(activitythread.java:5086)             @ java.lang.reflect.method.invokenative(native method)             @ java.lang.reflect.method.invoke(method.java:515)             @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:785)             @ com.android.internal.os.zygoteinit.main(zygoteinit.java:601)             @ dalvik.system.nativestart.main(native method) 

but, when use takepicture() method of camera object inside button click capture picture.

how can take picture without view click ?

you don't call startpreview method of camera object. think should call startpreview before take picture. in case didn't set preview display:

try {    camera.setpreviewdisplay(surface.getholder()); } catch (ioexception e) {    // todo auto-generated catch block    e.printstacktrace(); } 

check answer: how take pictures camera without preview when app starts?


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -