我们在项目开发中,相机是一个比较常用的东西,那么,我们一般如何使用呢


android中相机的使用有两种,一种是调用系统的相机来实现,另一种是我们自定义的相机来实现,那么我们第一步先来了解下使用系统的相机,当然,这种方式是很常用的,所以我在这里就简单地描述一下了


了解android的相机当然是去android得官网了解是最好的

手机相册的基本使用_第1张图片

可以看到在官网上面主要的是Camera和camera2这两个类,camera是从5.0开始添加的相机类,而Camera则从5.0开始不被推荐使用,至于使用哪种就是自己取决了



然后是拍照权限方面的说明

手机相册的基本使用_第2张图片

可以看到,关于相机的一些权限配置主要有两个

<uses-permission android:name="android.permission.CAMERA" />
这一项权限的话很暧昧,因为我们可以看到下面一句

Note:If you are using the cameravia an intent, your application does not need to request this permission.

就是说,如果我们通过调用系统的相机去拍照的话,其实是不需要生命这项权限的,这一点我们是需要验证一下的


第二个权限

<uses-feature android:name="android.hardware.camera" />
可以看到下面也有说明,说这一项的生命用于保证你的应用只安装在有相机的硬件或者支持相机的手机上,那么就意味着,在没有相机的手机上就安装不了你的app了,但是,表现形式是怎样的呢,话说还真找不到没有相机的手机。。。

不过,如果就因为没有相机就安装不了你的手机,那不是太坑爹了吗,下面有说明了

If your applicationcan usea camera or camera feature for proper operation, but does notrequireit, you should specify this in the manifest by including theandroid:requiredattribute, and setting it tofalse:

<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;@Overridepublic 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() {@Overridepublic 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>一般只对你的APP发布在GooglePlay的时候其作用,它协助GooglePlay来过滤您的应用程序,比如你明确的在你的程序清单中描述了你的程序必须使用哪些硬件或者软件相关的功能,则如果某些设备在GooglePlay上搜索应用时或者在某个程序的详情页上就会过滤掉不支持你的设备的程序。

就是说,那个权限只在GooglePlay商店起作用,在这里了解到了上面我的疑惑,不让没有摄像装备的手机安装的表现形式,原来是这么一回事 结论是
用于GooglePlay商店中不让没有相机设备的手机下载
<uses-feature android:name="android.hardware.camera"/>

那调用系统相机的话,实际上基本上什么权限都不用设置了(不准备在GooglePlay发布项目的话)
在这里,我还了解到了一种系统的浏览图片的方式,那就是
Intent intent = new Intent(Intent.ACTION_VIEW);Uri uri = Uri.fromFile(file); intent.setDataAndType(uri, "image/*");startActivity(intent);

不过在测试之后发现是调用默认相册或第三方图片查看工具的作用,一直会调用到微信的分享图片,所以实用性比较差,在此仅作为一种记录了





更多相关文章

  1. Android SELinux开发入门指南之正确姿势解决访问data目录权限问
  2. Android调用系统分享和指定app分享-微信朋友圈图文分享和qq分享
  3. Android BSP成长计划随笔之虚拟设备搭建和input系统
  4. VisionMobile:移动平台生态系统冲突报告(八)Chapter C:Android(下)
  5. Ubuntu编译Android系统源码问题记录

随机推荐

  1. Android 下拉框第三方控件 NiceSpinner
  2. 【Android okhttp源码解析 二】同步请求
  3. Android基于XMPP Smack openfire 开发的
  4. Android 分享文本和图片
  5. Android ScrollView的使用
  6. Android(安卓)Intent Action 大全(转)
  7. FIDO框架分析3(FIDO UAF Android客户端)
  8. h5页面点击按钮,触发手机自带的发短信
  9. Android如何获取视频预览图(或首帧)和获取
  10. Android AppCompatActivity的ActionBar之