1

在三星 S3 4.0.4 上运行以下TextureView 演示会在其他设备上产生黑屏,它工作得很好。onSurfaceTextureAvailable回调没有被调用,相机也没有启动。

添加android:hardwareAccelerated="true"到清单中的活动标签会产生以下错误,onSurfaceTextureAvailable但会被调用。

我做错了什么吗?有解决方法吗?

堆栈跟踪

03-04 13:26:53.210  23410-23410/com.example.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Fail to connect to camera service
            at android.hardware.Camera.native_setup(Native Method)
            at android.hardware.Camera.<init>(Camera.java:349)
            at android.hardware.Camera.open(Camera.java:326)
            at com.example.myapplication.LiveCameraActivity.onSurfaceTextureAvailable(LiveCameraActivity.java:25)
            at android.view.TextureView.getHardwareLayer(TextureView.java:343)
            at android.view.ViewGroup.drawChild(ViewGroup.java:2877)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
            at android.view.View.draw(View.java:11170)
            at android.widget.FrameLayout.draw(FrameLayout.java:450)
            at android.view.View.getDisplayList(View.java:10606)
            at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
            at android.view.View.getDisplayList(View.java:10604)
            at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
            at android.view.View.draw(View.java:11170)
            at android.widget.FrameLayout.draw(FrameLayout.java:450)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2298)
            at android.view.View.getDisplayList(View.java:10606)
            at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:842)
            at android.view.ViewRootImpl.draw(ViewRootImpl.java:2108)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1832)
            at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2644)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4517)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
            at dalvik.system.NativeStart.main(Native Method)

代码片段

package com.example.myapplication;

import android.app.Activity;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.TextureView;

import java.io.IOException;

public class LiveCameraActivity extends Activity implements TextureView.SurfaceTextureListener {
    private Camera mCamera;
    private TextureView mTextureView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTextureView = new TextureView(this);
        mTextureView.setSurfaceTextureListener(this);

        setContentView(mTextureView);
    }

    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        mCamera = Camera.open();

        try {
            mCamera.setPreviewTexture(surface);
            mCamera.startPreview();
        } catch (IOException ioe) {
            // Something bad happened
        }
    }

    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        // Ignored, Camera does all the work for us
    }

    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        mCamera.stopPreview();
        mCamera.release();
        return true;
    }

    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        // Invoked every time there's a new Camera preview frame
    }
}
4

2 回答 2

1

硬件加速对于TextureView. 抛出异常是因为我<uses-feature android:name="android.hardware.camera" />在 AndroidManifest.xml 中丢失了。问题是,硬件加速应该默认在 4.0.4 上启用。

于 2015-03-06T14:06:12.970 回答
0

该问题与 TextureView 无关。java.lang.RuntimeException:无法连接到相机服务。Camera.open()当另一个应用程序正在使用相机时,或者您之前在代码中调用并忘记释放它时,会引发此异常。

您应该打开相机并在或中onResume释放。onPauseonStart/onStop

于 2015-03-04T14:27:26.613 回答