當我嘗試計算攝像機的數量時,出現一個奇怪的錯誤。android獲取設備上攝像機的數量
的錯誤是:
「呼叫需要API 9級(curret min的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
}
}
你爲什麼不打算髮布錯誤? –
請隨意實際登錄您所得到的異常。 – CommonsWare
這是什麼「奇怪的錯誤」?你有LogCat輸出嗎? –