我们在项目开发中,相机是一个比较常用的东西,那么,我们一般如何使用呢
android中相机的使用有两种,一种是调用系统的相机来实现,另一种是我们自定义的相机来实现,那么我们第一步先来了解下使用系统的相机,当然,这种方式是很常用的,所以我在这里就简单地描述一下了
了解android的相机当然是去android得官网了解是最好的
可以看到在官网上面主要的是Camera和camera2这两个类,camera是从5.0开始添加的相机类,而Camera则从5.0开始不被推荐使用,至于使用哪种就是自己取决了
然后是拍照权限方面的说明
可以看到,关于相机的一些权限配置主要有两个
<uses-permission android:name="android.permission.CAMERA" />这一项权限的话很暧昧,因为我们可以看到下面一句
Note: If you are using the camera via an intent, your application does not need to request this permission.
就是说,如果我们通过调用系统的相机去拍照的话,其实是不需要生命这项权限的,这一点我们是需要验证一下的
第二个权限
<uses-feature android:name="android.hardware.camera" />可以看到下面也有说明,说这一项的生命用于保证你的应用只安装在有相机的硬件或者支持相机的手机上,那么就意味着,在没有相机的手机上就安装不了你的app了,但是,表现形式是怎样的呢,话说还真找不到没有相机的手机。。。
不过,如果就因为没有相机就安装不了你的手机,那不是太坑爹了吗,下面有说明了
If your application can use a camera or camera feature for proper operation, but does not require it, you should specify this in the manifest by including the android:required
attribute, and setting it to false
:
<uses-feature android:name="android.hardware.camera" android:required="false" />就是说,如果你手机需要相机功能,但是并不希望因为没有相机就导致应用无法安装的话,就这样声明吧
那么总的来说,对于相机的权限声明,就是:
一、在只需要调用系统的相机时
<uses-feature android:name="android.hardware.camera" android:required="false" />基本上只需要这样声明就行了,如果你希望没有相机的手机就不安装,那把后面的required=false去掉就行了
那么下一步就是调用系统的相机拍照了
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100; private Uri fileUri; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // create Intent to take a picture and return control to the calling application Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name // start the image capture Intent startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); }上面是google官方给出的拍照的代码,那么我们自己也可以写一个,顺便验证一下那个拍照权限的问题
那么下面是我的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.btn);
showImage = (Button)findViewById(R.id.showImage);
image = (ImageView)findViewById(R.id.image);
String path = getExternalCacheDir().getAbsolutePath() + "11.jpg";
Log.i("tag", "path:" + path);
file = new File(path);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri uri = Uri.fromFile(file); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, 100);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 100:
image.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()));
break;
default:
break;
}
}
manifest中的权限声明
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
实验结果是,调用系统的相机的确是不需要第一个权限声明的
然后我实验了一下
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera"/>
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
不过在测试之后发现是调用默认相册或第三方图片查看工具的作用,一直会调用到微信的分享图片,所以实用性比较差,在此仅作为一种记录了