三星Galaxy Nexus手机上LED手电筒无法正常工作

8

我遇到了以下问题:我的手电筒应用在三星Galaxy S2上运行良好,但不幸的是在三星Galaxy Nexus上无法正常工作(问题:手电筒忽略按钮点击->没有反应、没有光、没有崩溃、没有异常)。我在stackoverflow上阅读了“Galaxy Nexus的LED手电筒由哪个API可控制?”但它并没有帮助我解决问题,因为我的问题仍然存在。这是我控制灯光的代码片段:

final Button FlashLightControl = (Button)findViewById(R.id.ledbutton);
FlashLightControl.setOnClickListener(new Button.OnClickListener()
{
        public void onClick(View arg) 
        {
            if(camera != null)
            {
                //in case light is on we will turn it off
                parameters = camera.getParameters();
                parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
                camera.setParameters(parameters);
                camera.stopPreview();
                camera.release();
                camera = null;
            }
            else
            {
                // light is off - we turn it on
                camera = Camera.open();
                parameters = camera.getParameters();
                parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
                camera.setParameters(parameters);
                camera.startPreview();
            }
        }}); 

有任何想法吗?为了完整起见 - 这些是我在Androidmanifest.xml中添加的权限:

    <uses-feature android:name="android.hardware.camera.flash" />
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>

有人能帮忙吗?

祝好, CarpeTemporem


我也想知道。 - Pedro Rainho
2个回答

17

我也遇到了同样的问题,但是我想要从一个服务中打开LED,所以我不能使用一个1x1的SurfaceView。下面是我做的使它可以工作的方法。

private void turnLEDOn() throws IOException
{
    // In order to work, the camera needs a surface to turn on.
    // Here I pass it a dummy Surface Texture to make it happy.
    camera = Camera.open();
    camera.setPreviewTexture(new SurfaceTexture(0));
    camera.startPreview();
    Parameters p = camera.getParameters();
    p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    camera.setParameters(p);
}

private void turnLEDOff()
{
    if (camera != null)
    {
        // Stopping the camera is enough to turn off the LED
        camera.stopPreview();
        camera.release();
        camera = null;
    } else
        throw new NullPointerException("Camera doesn't exist to turn off.");

}

SurfaceTexture是在API Level 11(Android 3.0)中添加的,因此它只能在Honeycomb或更新版本上使用。对于旧的API级别,您可以使用其他答案中的SurfaceView技巧。


1
new SurfaceTexture(0) 在我的Nexus 5上运行着原生的Android 5.1系统,非常感谢! - shkschneider

4

我曾经遇到同样的问题,通过使用一个宽度和高度均为1像素的Surface View解决了这个问题。


4
1px/1px表面视图是什么意思?意思是显示一个像素点的大小来查看网页或应用程序上的元素。其中,1px指的是像素的大小,通常是屏幕上最小的可见单位。在表面视图中,这个像素点会被放大以便更清晰地显示细节。 - Last Warrior
@LastWarrior SurfaceView 有一个高度为1像素,宽度为1像素的视图,放置在某个位置,并调用 surfaceview 的回调函数。 - George Spasov
对于那些想知道Surface视图的人,这个答案可以澄清一下:https://dev59.com/tGox5IYBdhLWcg3w74u3 - AgentKnopf
然而,在SGSII上仍然无法工作xD...在Sony XPeria S上可以工作,但可能在那里也可以没有表面材料。 - AgentKnopf
@GeorgeSpasov 我可以在活动中实现 Surface View,但如何为小部件做到呢? - Shakeeb Ayaz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接