如何解决ANR错误,而调用摄像机?如何解决、摄像机、错误、ANR

2023-09-13 01:59:40 作者:口吻生花

我有两个按钮在主菜单。我调用相机时,我preSS 1号键。在这里,我没有得到任何问题。相机工作正常。拍摄照片后,我回来到主菜单,然后再次我preSS 1号键。在这里,我得到了这个问题。摄像头调用正常。但我得到了 ANR错误(原因:keyDispatchingTimedOut)而我拍照。如何解决这个问题?

修改::

我用下面的code,

按钮监听器

 按钮的ImageButton =(按钮)findViewById(R.id.button1);
imageButton.setOnClickListener(新View.OnClickListener(){

    公共无效的onClick(查看为arg0){
    意向意图=新的意图();
    intent.setClass(活动,ImageActivity.class);
    startActivity(意向);
    }
});
 
松下发布模块化摄像机BGH1 主动散热设计可无限制录制

ImageActivity.java

 公共类ImageActivity扩展活动实现SurfaceHolder.Callback {
    私人相机拍照= NULL;
    私人SurfaceHolder surfaceHolder = NULL;
    私人布尔previewRunning = FALSE;
    私人按钮btnDone,btnCapture,btnRetake;
    私人位图mBitmap;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        。getWindow()和setFormat(PixelFormat.TRANSLUCENT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        的setContentView(R.layout.surface_screen);
        SurfaceView surfaceView =(SurfaceView)findViewById(R.id.camera preVIEW);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(本);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        surfaceHolder.setFixedSize(getWindow()。getWindowManager()
                .getDefaultDisplay()。的getWidth(),getWindow()。getWindowManager()
                .getDefaultDisplay()的getHeight())。
        LayoutInflater controlInflater = LayoutInflater.from(getBaseContext());

        最后查看viewControl = controlInflater.inflate(R.layout.control,NULL);
        的LayoutParams layoutParamsControl =新的LayoutParams(
                LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
        activity.addContentView(viewControl,layoutParamsControl);

        btnCapture =(按钮)findViewById(R.id.takepicture);

        btnDone =(按钮)findViewById(R.id.send);

        btnCapture.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(视图v){
                camera.takePicture(NULL,picCalBac,picCalBac);
            }
        });


    Camera.PictureCallback picCalBac =新PictureCallback(){

        公共无效onPictureTaken(byte []的数据,摄像头摄像头){
            如果(数据!= NULL){
                mBitmap = BitmapFactory.de codeByteArray(数据,0,data.length);
            }
        }
    };

    公共无效surfaceChanged(SurfaceHolder持有人,INT格式,INT W,INT高){
        如果(previewRunning){
            camera.stop preVIEW();
        }
        尝试 {
            camera.set previewDisplay(surfaceHolder);
        }赶上(IOException异常E){
            Log.d(IOException异常,e.getMessage());
        }
        camera.start preVIEW();
        previewRunning = TRUE;
    }

    公共无效surfaceCreated(SurfaceHolder为arg0){
        相机= Camera.open(0);
    }

    公共无效surfaceDestroyed(SurfaceHolder为arg0){
        camera.stop preVIEW();
        previewRunning = FALSE;
        camera.release();
    }
}
 

解决方案

您可能已经通过的这个链接同时寻找错误。

虽然我从未有过这样的问题,阅读在互联网上后,这是我的理解:

说明:

  

ANR或应用程序无响应的错误发生在一个进程   在主线程上花费的时间太长(像> 5秒)。安卓   杀死该进程以及任何设计相关的备用设备的   资源。

     

的解决方案是在另一个线程上运行的资源昂贵的任务,并   然后发布或更新相应的主线程。

请参阅此: Android的ANR keyDispatchingTimedOut

所以,你的情况,你可能的相互矛盾的两个不同的线程的从不断的长时间运行一个和Android杀死他们。接下来的相关code也被影响了。

因此​​,更好地分类您的code,写在不同的发每一个新的任务,处理程序键,如果你正在做一个UI的任务,使用 runOnUIThread 异步任务也是非常方便的。

另一件事是设法去除其他的code依赖。写一些默认值,并从您可以发送用户返回到第一个任务,如果它没有做好。

我相信错误是从你的编码风格,不会因为你的code任何特定的错误。

您需要提高你的那个特定的code能够有效地执行,并为看到这些2链接:

设计响应 无痛线程

编辑:

这个我曾经读过,发现有效的是,

如何调查ANR?

首先,去了你的code和寻找vunerable点和长时间运行的操作。实例可以包括使用套接字,锁,螺纹睡觉,和其它阻塞操作从事件线程内。您应该确保这些都发生在单独的线程。如果似乎没有任何问题,使用DDMS,并启用线程视图。这将显示所有类似跟踪你有线程应用程序中。重现ANR,并刷新主线程在同一时间。这应该告诉你precisely什么的ANR

时怎么回事

此外,如果ANR是因为线程造成的?

您可以为用户服务, 所以,你的应用程序可以做费时内service.onStart任务() 在用于启动服务的意图传递数据(例如)。

然而,服务的主应用程序线程上执行。如果利用独立的 螺纹是必要的,它可以由服务内ONSTART()创建的。

已经有一个内置的类,这是否: IntentService

也找到了一个有用的库应用程序的 SalomonBrys / ANR-看门狗

I have two button's in the main menu. I invoking the camera when I press the 1st button. Here I didn't get any issue. Camera working properly. After taking the picture, I come back in to main menu and again I press the 1st button. Here I got the issue. Camera invoking properly. But I got ANR error (Reason: keyDispatchingTimedOut) while i'm taking the picture. How to resolve this issue?

Edit::

I'm using following code,

Button Listener,

Button imageButton = (Button) findViewById(R.id.button1);
imageButton.setOnClickListener(new  View.OnClickListener() {

    public void onClick(View arg0) {
    Intent intent = new Intent();
    intent.setClass(activity, ImageActivity.class);
    startActivity(intent);
    }
});

ImageActivity.java

public class ImageActivity extends Activity implements SurfaceHolder.Callback {
    private Camera camera = null;
    private SurfaceHolder surfaceHolder = null;
    private boolean previewRunning = false;
    private Button btnDone, btnCapture, btnRetake;
    private Bitmap mBitmap;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.surface_screen);
        SurfaceView surfaceView = (SurfaceView) findViewById(R.id.camerapreview);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        surfaceHolder.setFixedSize(getWindow().getWindowManager()
                .getDefaultDisplay().getWidth(), getWindow().getWindowManager()
                .getDefaultDisplay().getHeight());
        LayoutInflater  controlInflater = LayoutInflater.from(getBaseContext());

        final View viewControl = controlInflater.inflate(R.layout.control, null);
        LayoutParams layoutParamsControl = new LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        activity.addContentView(viewControl, layoutParamsControl);

        btnCapture = (Button) findViewById(R.id.takepicture);

        btnDone = (Button) findViewById(R.id.send);

        btnCapture.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                camera.takePicture(null, picCalBac, picCalBac);
            }
        });


    Camera.PictureCallback picCalBac = new PictureCallback() {

        public void onPictureTaken(byte[] data, Camera camera) {
            if (data != null) {
                mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
            }
        }
    };

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        if (previewRunning) {
            camera.stopPreview();
        }
        try {
            camera.setPreviewDisplay(surfaceHolder);
        } catch (IOException e) {
            Log.d("IOException", e.getMessage());
        }
        camera.startPreview();
        previewRunning = true;
    }

    public void surfaceCreated(SurfaceHolder arg0) {
        camera = Camera.open(0);
    }

    public void surfaceDestroyed(SurfaceHolder arg0) {
        camera.stopPreview();
        previewRunning = false;
        camera.release();
    }
}

解决方案

You might have gone through this link while searching for the error.

Though I never had such issue, after reading on the Internet, this is what I understand:

Description :

ANR or Application Not Responding error occurs when a process on the main thread takes too long (something like > 5 seconds). Android kills that process and any related by design to spare the device's resources.

The solution is to run resource-expensive tasks on another thread, and then post, or update the main thread accordingly.

See This : Android ANR keyDispatchingTimedOut

So in your case you may be conflicting two different threads from the one that keeps running for long time and Android kills them. The next related code also gets affected.

So better classify your code, write each new task in different Thread,Handler and if you're doing a UI task, use runOnUIThread. Async Task is also very Handy.

Another thing is to try to remove dependency of code on other. Write some default values and from that you can send user back to first task if it's not done properly.

I believe the error is from your coding Style, not because of any specific error in your code.

You need to improve your that particular code to perform efficiently and for that see these 2 links:

Design for Responsiveness Painless Threading

EDIT:

This I read somewhere and found effective is,

How to investigate ANR ?

First, go over your code and look for vunerable spots and long running operations. Examples may include using sockets, locks, thread sleeps, and other blocking operations from within the event thread. You should make sure these all happen in separate threads. If nothing seems the problem, use DDMS and enable the thread view. This shows all the threads in your application similar to the trace you have. Reproduce the ANR, and refresh the main thread at the same time. That should show you precisely whats going on at the time of the ANR

Also If ANR is caused because of Threads ?

you can user Service for that, So your app can do the time consuming tasks inside service.onStart(), passing data (for example) in the intent used to start the service.

However, Services execute on the main application thread. If a separate thread is needed, it can be created by the service inside onStart().

There is already a built-in class that does this: IntentService

Also found one helpful library application SalomonBrys/ANR-WatchDog