android - Open camera directly in the Activity without clicking on button and without intent? -
i trying open camera without clicking on button doesn't work...
i follow instructions here : http://developer.android.com/guide/topics/media/camera.html#custom-camera
here result :
when press button "capture", activates camera want activate camera before , record when click on button snapchat. think miss somewhere when open camera don't find mistakes...
here class :
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); preview = (framelayout) findviewbyid(r.id.camera_preview); if (checkcamerahardware(this)) { mcamera = getcamerainstance(); mpreview = new camerapreview(this, mcamera); mcamera.setdisplayorientation(90); initbutton(); preview.addview(mpreview); preview.removeallviews(); preview.addview(mpreview); preview.addview(capturebutton); } }
after that, have method initiate button :
public void initbutton () { capturebutton = (button) findviewbyid(r.id.button_capture); capturebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (isrecording) { // stop recording , release camera mmediarecorder.stop(); releasemediarecorder(); mcamera.lock(); // inform user recording has stopped capturebutton.settext("capture"); isrecording = false; } else { // initialize video camera if (preparevideorecorder()) { // camera available , unlocked, mediarecorder prepared, // can start recording mmediarecorder.start(); capturebutton.settext("stop"); isrecording = true; } else { releasemediarecorder(); toast.maketext(mainactivity.this, "camera doesn't work", toast.length_long).show(); } } } }); }
here method instance of camera :
public static camera getcamerainstance() { camera c = null; try { c = camera.open(); } catch (exception e) { log.i("camera info : >", "camera doesn't exist"); } return c; }
here method prepare record :
private boolean preparevideorecorder() { mmediarecorder = new mediarecorder(); // step 1: unlock , set camera mediarecorder mcamera.unlock(); mmediarecorder.setcamera(mcamera); // step 2: set sources mmediarecorder.setaudiosource(mediarecorder.audiosource.camcorder); mmediarecorder.setvideosource(mediarecorder.videosource.camera); // step 3: set camcorderprofile (requires api level 8 or higher) camcorderprofile camcorderprofile = camcorderprofile.get(camcorderprofile.quality_high); mmediarecorder.setprofile(camcorderprofile); // step 4: set output file mmediarecorder.setoutputfile(getoutputmediafile(capture_video_file).tostring()); // step 5: set preview output mmediarecorder.setpreviewdisplay(mpreview.getholder().getsurface()); // step 6: prepare configured mediarecorder try { mmediarecorder.prepare(); } catch (illegalstateexception e) { log.d("prepare mediarecorder : >", ": > > illegalstateexception preparing mediarecorder: " + e.getmessage()); releasemediarecorder(); return false; } catch (ioexception e) { log.d("prepare mediarecorder : >", " : > > ioexception preparing mediarecorder: " + e.getmessage()); releasemediarecorder(); return false; } return true; }
then try open camera before click instantiation don't know how... if 1 has idea, useful me.
i found error. called mcamera.setpreview()
in onsurfacecreated()
method of camerapreview class.
i need call mcamera.setpreview()
in onsurfacechanged()
! works
problem solved !