Android9.0检测摄像头是否存在源码完整案例
程序员文章站
2022-06-09 13:28:17
...
关键代码如下,使用的是PackageManager去检查系统特性,其他设备也可以这样做的。
完整例子已在AndroidStudio3.5.2上运行通过:
PhoneTest.zip:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PackageManager packageManager = getApplicationContext().getPackageManager();
boolean hasCamera = packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA) || packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT);
if( hasCamera ){
Toast.makeText(this,"摄像头已经准备好了",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this,"摄像头找不到",Toast.LENGTH_LONG).show();
}
}