android - Custom camera not working on 4.0.4 -
i test code in android 2.2 , android 3.2. working perfect when run in android 4.0.4, not working. when click on button take picture, application not responding , going hang.
preview.java
package com.example; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.hardware.camera; import android.hardware.camera.previewcallback; import android.util.log; import android.view.surfaceholder; import android.view.surfaceview; class preview extends surfaceview implements surfaceholder.callback { private static final string tag = "preview"; surfaceholder mholder; public camera camera; preview(context context) { super(context); // install surfaceholder.callback notified when // underlying surface created , destroyed. mholder = getholder(); mholder.addcallback(this); mholder.settype(surfaceholder.surface_type_push_buffers); } public void surfacecreated(surfaceholder holder) { // surface has been created, acquire camera , tell // draw. camera = camera.open(); try { camera.setpreviewdisplay(holder); camera.setpreviewcallback(new previewcallback() { public void onpreviewframe(byte[] data, camera arg1) { fileoutputstream outstream = null; try { outstream = new fileoutputstream(string.format("/sdcard/%d.jpg", system.currenttimemillis())); outstream.write(data); outstream.close(); log.d(tag, "onpreviewframe - wrote bytes: " + data.length); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } { } preview.this.invalidate(); } }); } catch (ioexception e) { e.printstacktrace(); } } public void surfacedestroyed(surfaceholder holder) { // surface destroyed when return, stop preview. // because cameradevice object not shared resource, it's // important release when activity paused. camera.stoppreview(); camera = null; } public void surfacechanged(surfaceholder holder, int format, int w, int h) { // size known, set camera parameters , begin // preview. camera.parameters parameters = camera.getparameters(); // parameters.setpreviewsize(w, h); camera.setparameters(parameters); camera.startpreview(); } @override public void draw(canvas canvas) { super.draw(canvas); paint p= new paint(color.red); log.d(tag,"draw"); canvas.drawtext("preview", canvas.getwidth()/2, canvas.getheight()/2, p ); } }
camerademo.java
package com.example; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import android.app.activity; import android.content.intent; import android.hardware.camera; import android.hardware.camera.picturecallback; import android.hardware.camera.shuttercallback; import android.os.bundle; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.framelayout; public class camerademo extends activity { private static final string tag = "camerademo"; camera camera; preview preview; button buttonclick; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); preview = new preview(this); ((framelayout) findviewbyid(r.id.preview)).addview(preview); buttonclick = (button) findviewbyid(r.id.buttonclick); buttonclick.setonclicklistener( new onclicklistener() { public void onclick(view v) { preview.camera.takepicture(shuttercallback, rawcallback, jpegcallback); } }); log.d(tag, "oncreate'd"); } shuttercallback shuttercallback = new shuttercallback() { public void onshutter() { log.d(tag, "onshutter'd"); } }; /** handles data raw picture */ picturecallback rawcallback = new picturecallback() { public void onpicturetaken(byte[] data, camera camera) { log.d(tag, "onpicturetaken - raw"); } }; /** handles data jpeg picture */ picturecallback jpegcallback = new picturecallback() { public void onpicturetaken(byte[] data, camera camera) { fileoutputstream outstream = null; long time = 0; try { // write local sandbox file system // outstream = camerademo.this.openfileoutput(string.format("%d.jpg", system.currenttimemillis()), 0); // or write sdcard time = system.currenttimemillis(); outstream = new fileoutputstream(string.format("/sdcard/%d.jpg",time)); outstream.write(data); outstream.close(); log.d(tag, "onpicturetaken - wrote bytes: " + data.length); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } { } log.d(tag, "onpicturetaken - jpeg"); } }; }
main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/layout"> <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="camera demo" android:textsize="24sp" /> <framelayout android:id="@+id/preview" android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="fill_parent"> </framelayout> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/buttonclick" android:text="click" android:layout_gravity="center"></button> </linearlayout>
please don't use line "/sdcard". dont hard code in code instead use neat approach calling environment.getexternalstoragedirectory() method. think might problem. please check , confirm.
Comments
Post a Comment