Android 安卓在找相机吗
我正试图遵循android开发文档中关于构建摄像头应用程序的说明。因此,文档中的第一步是查看应用程序所在的设备是否有摄像头。因此,我构建了一个简单的代码,可以搜索我的设备(在本例中是模拟器),并确定它是否有摄像头。如果确实创建TextVew并显示“是”,如果不创建TextVew并显示“否”。但它所做的只是运行并在main layout.xml中显示文本,即“hello world”。我的代码张贴在下面,任何帮助都将被通知Android 安卓在找相机吗,android,camera,Android,Camera,我正试图遵循android开发文档中关于构建摄像头应用程序的说明。因此,文档中的第一步是查看应用程序所在的设备是否有摄像头。因此,我构建了一个简单的代码,可以搜索我的设备(在本例中是模拟器),并确定它是否有摄像头。如果确实创建TextVew并显示“是”,如果不创建TextVew并显示“否”。但它所做的只是运行并在main layout.xml中显示文本,即“hello world”。我的代码张贴在下面,任何帮助都将被通知 package com.example.cam_test2; imp
package com.example.cam_test2;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
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);
}
@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;
}
}
}您应该在onCreate方法中调用checkCameraHardware(this),以便在启动“活动”时运行它。试试这个
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkCameraHardware(getApplicationContext());
}
你们有并没有尝试为清单文件添加权限?你们有并没有搜索任何摄像头的例子并自己尝试?我添加了权限,但仍然并没有。。。