我无法让相机打开它的灯

时间:2014-02-02 17:49:27

标签: android android-camera oncreate user-permissions

我有一个简单的一类应用程序应该打开相机的灯,但这是我的错误日志;

02-02 12:40:47.939: W/dalvikvm(24314): threadid=1: thread exiting with uncaught exception (group=0x4178d898)
02-02 12:40:47.949: E/AndroidRuntime(24314): FATAL EXCEPTION: main
02-02 12:40:47.949: E/AndroidRuntime(24314): java.lang.NullPointerException
02-02 12:40:47.949: E/AndroidRuntime(24314):    at de.vogella.android.notificationmanager.NotificationReceiverActivity.onClick(NotificationReceiverActivity.java:53)
02-02 12:40:47.949: E/AndroidRuntime(24314):    at android.view.View.performClick(View.java:4475)
02-02 12:40:47.949: E/AndroidRuntime(24314):    at android.view.View$PerformClick.run(View.java:18784)
02-02 12:40:47.949: E/AndroidRuntime(24314):    at android.os.Handler.handleCallback(Handler.java:730)
02-02 12:40:47.949: E/AndroidRuntime(24314):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-02 12:40:47.949: E/AndroidRuntime(24314):    at android.os.Looper.loop(Looper.java:137)
02-02 12:40:47.949: E/AndroidRuntime(24314):    at android.app.ActivityThread.main(ActivityThread.java:5414)
02-02 12:40:47.949: E/AndroidRuntime(24314):    at java.lang.reflect.Method.invokeNative(Native Method)
02-02 12:40:47.949: E/AndroidRuntime(24314):    at java.lang.reflect.Method.invoke(Method.java:525)
02-02 12:40:47.949: E/AndroidRuntime(24314):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
02-02 12:40:47.949: E/AndroidRuntime(24314):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
02-02 12:40:47.949: E/AndroidRuntime(24314):    at dalvik.system.NativeStart.main(Native Method)

这是我的班级;

import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;

public class NotificationReceiverActivity extends Activity implements OnClickListener {
    private final String CLASSNAME = getClass().getSimpleName();
    boolean camOn = false;
    public boolean started;

Camera cam = null;
ImageButton ib1;
Parameters para;
PowerManager pm;
WakeLock wl;
@Override
protected void onCreate(Bundle savedInstanceState) {
started = true;
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "whatever");
super.onCreate(savedInstanceState);
setContentView(R.layout.result);

wl.acquire();
initialize();
ib1.setOnClickListener(this);
Log.i(CLASSNAME, "CREATING NOW  "+camOn);
}

private void initialize() {
// TODO Auto-generated method stub
ib1 = (ImageButton) findViewById(R.id.ib2);
}

public void onClick(View v) {

// TODO Auto-generated method stub
if (camOn == false) {
       cam.release();

    cam = Camera.open();
    para = cam.getParameters();
    para.setFlashMode(Parameters.FLASH_MODE_TORCH);
    cam.setParameters(para);
    Log.i(CLASSNAME, "AA1  "+camOn);
    camOn = true;
    Log.i(CLASSNAME, "AA2  "+camOn);

} 



else {
    para.setFlashMode(Parameters.FLASH_MODE_OFF);
    cam.setParameters(para);
    cam.release();
    //cam = null;
    Log.i(CLASSNAME, "BB1  "+camOn);
    camOn = false;
    Log.i(CLASSNAME, "BB2  "+camOn);

}
}

@Override
protected void onPause() {
super.onPause();
// TODO Auto-generated method stub
cam.release();
//  cam=cam;
// finish();*/
}
@Override
protected void onStop() {
super.onStop();
// cam.release();

}
@Override
protected void onDestroy() {
super.onDestroy();
// cam.release();

}
protected void onResume() {
super.onResume();
//cam = Camera.open();
}

}

如果我在Android 4.1.2上运行这个相同的完全代码,它可以完美地工作,但是在我的Android Galaxy note3上,它是4.3,它显示了GUI,当你点击按钮来打开/关闭相机灯时没有做任何事情,并按照你的看法给了我一只原木猫。

这是布局文件;

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Flash" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="147dp"
        android:layout_marginBottom="15dip"
        android:gravity="center"
        android:padding="15dip"
        android:text="\n\n\n\n\n"
        android:textColor="#3b3b3b"
        android:textSize="13dip" />

    <ImageButton
        android:id="@+id/ib2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="15dip"
        android:gravity="center"
        android:padding="15dip"
        android:text="\n"
        android:textColor="#3b3b3b"
        android:textSize="13dip" />

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

我发现了一些事情。

首先,当camOn == false cam.release()时,即使cam=NullsurfaceView也是msurfaceView = (SurfaceView)findViewById(R.id.surfaceView); msurfaceHolder = surfaceView.getHolder(); msurfaceHolder.addCallback(this); msurfaceHolder.setFixedSize(1, 1); 。如果我在那里遗漏了一些东西,那很好。

第二个是你实际上需要一个setParameters元素才能使用相机。有些设备没有它,但它是android系统的一部分,详见here

例如,在将layoutView添加到布局后,您可以执行

mCamera.setPreviewDisplay(mSurfaceHolder);
mCamera.startPreview();

在使用{{1}}设置摄像机参数后,请调用

{{1}}

这是我手机上的一个问题,使用表面视图解决了这个问题。我假设你已经编辑了清单文件以包含正确的权限。