【问题标题】:android getting the number of cameras on a device [duplicate]android获取设备上的摄像头数量[重复]
【发布时间】:2014-01-08 00:38:06
【问题描述】:

我在尝试计算摄像头数量时遇到一个奇怪的错误。

错误是:

“调用需要 API 级别 9(当前最小值为 5):android.hardware.Camera#getNumberOfCameras”

package com.example.cam_test2;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 checkCameraHardware(getApplicationContext());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

/** Check if this device has a camera */
private boolean checkCameraHardware(Context context) {
    if   (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
        // this device has a camera
        LinearLayout lView = new LinearLayout(this);

        TextView myText = new TextView(this);

        myText.setText("yes");

        lView.addView(myText);

        setContentView(lView);
        return true;
    } else {
        // no camera on this device
        LinearLayout lView = new LinearLayout(this);

        TextView myText = new TextView(this);
        myText.setText("no");

        lView.addView(myText);

        setContentView(lView);

        return false;
    }
}

/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
    int camNum;
    camNum=Camera.getNumberOfCameras() ;///////error here///////////

    TextView myTextView = null;
    myTextView.setText("There are " + camNum+" cameras availabe");

    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e){
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}
}

【问题讨论】:

  • 你为什么不费心发布错误?
  • 请随时实际记录您遇到的异常。
  • 这是什么“特殊错误”?你有 LogCat 输出吗?
  • 我刚刚编辑了我的帖子并添加了错误..
  • 是的,这绝对是我发布的那个的副本。

标签: android android-camera


【解决方案1】:

您需要在 AndroidManifest 文件中请求使用相机的权限。

把这个放在<manifest>的根目录下:

<uses-permission android:name="android.permission.CAMERA"/>

您还需要确保指定了正确的最低 API 级别。此类是在 API 级别 9 中引入的,因此您需要将 9 或更高级别指定为最低 API 级别:

<uses-sdk android:minSdkVersion="9"/>

【讨论】:

  • 我将我的 android:minSDKVersion 设置为 5。我想你可以尽可能早地设置 sdk 版本并且它仍然可以工作?
  • @codenamejupiterx 不。要使某个功能正常工作,它必须具有支持的最低 API 级别。您正在考虑推荐的 API 级别。他们做出区分的原因是允许开发人员对某些功能进行优雅的降级。例如,多个摄像头与单个摄像头。
【解决方案2】:

如果您可以发布您的 Manifest 文件会有所帮助,但我认为您需要在其中提高 minSdkVersion。

<uses-sdk android:minSdkVersion="10" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-03
    • 1970-01-01
    • 2011-10-07
    • 2021-07-17
    相关资源
    最近更新 更多