我遇到了问题。当我拍摄多张照片时,我的活动将无法响应。有时甚至连第一个都有ANR问题。我不知道为什么。我根据DEV指南编写代码。这是我的代码:
public class CameraActivity extends Activity implements SurfaceHolder.Callback,OnClickListener,AutoFocusCallback{
private Button takeShot,goBack;
private SurfaceView surfaceView;
private SurfaceHolder holder;
private Camera camera;
private ArrayList<String> pictures;
private PictureDAO pdao;
public static final int PICTURE_OK=1554;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
pdao=new PictureDAO(this);
initComponent();
}
private void initComponent(){
surfaceView=(SurfaceView)findViewById(R.id.cameraView);
holder=surfaceView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
takeShot=(Button)findViewById(R.id.lets_take_it);
goBack=(Button)findViewById(R.id.back_to_tab3_button);
takeShot.setOnClickListener(this);
goBack.setOnClickListener(this);
}
/**
* set camera parameters
*/
private void setCameraParameters(){
Parameters para=camera.getParameters();
para.setPictureFormat(ImageFormat.JPEG);
Size preSize=para.getSupportedPreviewSizes().get(para.getSupportedPreviewSizes().size()-1);
para.setPreviewSize(preSize.width, preSize.height);
Size picSize=para.getSupportedPictureSizes().get(0);
para.setPictureSize(picSize.width, picSize.height);
camera.setParameters(para);
camera.setDisplayOrientation(90);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
camera=Camera.open();
setCameraParameters();
try {
camera.setPreviewDisplay(holder);
} catch (Exception e) {
LogUtil.log(e.getMessage());
LogUtil.log(e.getLocalizedMessage());
e.printStackTrace();
}
camera.startPreview();
camera.autoFocus(this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
releaseCamera();
}
private void releaseCamera(){
if(camera!=null){
camera.stopPreview();
camera.release();
camera=null;
}
}
private void ICatchU(){
camera.takePicture(null, null, new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
camera.stopPreview();
Bitmap map =BitmapFactory.decodeByteArray(data, 0, data.length);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File picPath=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"whycools"+File.separator+"picture"+File.separator+DateUtil.getFormattedDate(DateUtil.JUST_DATE)+File.separator+timeStamp+".jpg");
try {
picPath.getParentFile().mkdirs();
picPath.createNewFile();
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(picPath));
map.compress(CompressFormat.JPEG, 80, bos);
pdao.insertPic(new PictureInfo(picPath.getAbsolutePath(), timeStamp));
if(pictures==null){
pictures=new ArrayList<String>();
}
System.out.println(picPath.getAbsolutePath());
pictures.add(picPath.getAbsolutePath());
bos.flush();
bos.close();
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
LogUtil.log(e.getMessage());
LogUtil.log(e.getLocalizedMessage());
e.printStackTrace();
}
camera.startPreview();
}
});
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK){
releaseCamera();
Intent n=new Intent(this,Tab3Activity.class);
n.putStringArrayListExtra("picData", pictures);
n.putExtra("shot_time", DateUtil.getFormattedDate(DateUtil.DATE_AND_MINUTE));
this.setResult(PICTURE_OK, n);
this.finish();
}
return super.onKeyUp(keyCode, event);
}
@Override
public void onClick(View v) {
int id=v.getId();
switch (id) {
case R.id.lets_take_it:ICatchU();break;
case R.id.back_to_tab3_button:{
releaseCamera();
Intent n=new Intent(this,Tab3Activity.class);
n.putStringArrayListExtra("picData", pictures);
n.putExtra("shot_time", DateUtil.getFormattedDate(DateUtil.DATE_AND_MINUTE));
this.setResult(PICTURE_OK, n);
this.finish();
};break;
}
}
private void showToastMessage(String msg){
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
@Override
public void onAutoFocus(boolean success, Camera camera) {
}
@Override
protected void onDestroy() {
super.onDestroy();
if(pdao!=null){
pdao.close();
pdao=null;
}
}
}
----- pid 200 at 2011-12-16 08:38:45 -----
Cmd line: com.google.process.gapps
DALVIK THREADS:
(mutexes: tll=0 tsl=0 tscl=0 ghl=0 hwl=0 hwll=0)
"main" prio=5 tid=1 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x4001f1a8 self=0xce48
| sysTid=200 nice=0 sched=0/0 cgrp=default handle=-1345006528
| schedstat=( 545418989 6032995440 141 )
at android.os.BinderProxy.transact(Native Method)
at android.os.IPowerManager$Stub$Proxy.acquireWakeLock(IPowerManager.java:276)
at android.os.PowerManager$WakeLock.acquire(PowerManager.java:253)
at com.google.android.gsf.checkin.CheckinService.launchService(CheckinService.java:167)
at com.google.android.gsf.checkin.CheckinService.access$000(CheckinService.java:64)
at com.google.android.gsf.checkin.CheckinService$Receiver.onReceive(CheckinService.java:101)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:1794)
at android.app.ActivityThread.access$2400(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
"RefQueueWorker@org.apache.http.impl.conn.tsccm.ConnPoolByRoute@4050cad0" daemon prio=5 tid=9 WAIT
| group="main" sCount=1 dsCount=0 obj=0x4055a8f0 self=0x8c270
| sysTid=220 nice=0 sched=0/0 cgrp=default handle=1013376
| schedstat=( 1148267 89253041 3 )
at java.lang.Object.wait(Native Method)
- waiting on <0x4054f928> (a java.lang.ref.ReferenceQueue)
at java.lang.Object.wait(Object.java:395)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:107)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:80)
at org.apache.http.impl.conn.tsccm.RefQueueWorker.run(RefQueueWorker.java:102)
at java.lang.Thread.run(Thread.java:1019)
"Binder Thread #2" prio=5 tid=8 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x40511c08 self=0x1ba2a0
| sysTid=213 nice=0 sched=0/0 cgrp=default handle=1717752
| schedstat=( 9471974 67681837 3 )
at dalvik.system.NativeStart.run(Native Method)
"Binder Thread #1" prio=5 tid=7 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x40511b40 self=0x185d30
| sysTid=212 nice=0 sched=0/0 cgrp=default handle=1717688
| schedstat=( 20164408 44832613 4 )
at dalvik.system.NativeStart.run(Native Method)
"Compiler" daemon prio=5 tid=6 VMWAIT
| group="system" sCount=1 dsCount=0 obj=0x4050edf8 self=0x185bf8
| sysTid=211 nice=0 sched=0/0 cgrp=default handle=971936
| schedstat=( 37893754 117636767 45 )
at dalvik.system.NativeStart.run(Native Method)
"JDWP" daemon prio=5 tid=5 VMWAIT
| group="system" sCount=1 dsCount=0 obj=0x4050ed10 self=0x1a33f8
| sysTid=210 nice=0 sched=0/0 cgrp=default handle=1597960
| schedstat=( 15809388 276306241 17 )
at dalvik.system.NativeStart.run(Native Method)
"Signal Catcher" daemon prio=5 tid=4 RUNNABLE
| group="system" sCount=0 dsCount=0 obj=0x4050ec50 self=0x186018
| sysTid=203 nice=0 sched=0/0 cgrp=default handle=588480
| schedstat=( 20070264 129928190 4 )
at dalvik.system.NativeStart.run(Native Method)
"GC" daemon prio=5 tid=3 VMWAIT
| group="system" sCount=1 dsCount=0 obj=0x4050eba8 self=0x186f58
| sysTid=202 nice=0 sched=0/0 cgrp=default handle=588416
| schedstat=( 112148122 1039499175 25 )
at dalvik.system.NativeStart.run(Native Method)
"HeapWorker" daemon prio=5 tid=2 VMWAIT
| group="system" sCount=1 dsCount=0 obj=0x4050eaf0 self=0x142738
| sysTid=201 nice=0 sched=0/0 cgrp=default handle=588352
| schedstat=( 105047309 2012821363 24 )
at dalvik.system.NativeStart.run(Native Method)
----- end 200 -----
答案 0 :(得分:0)
线程化您的onClick()
以避免锁定UI
。