基础Android之调用相机和相册

  因为Android6.0之后,Android系统的安全级别升级,我们的项目都需要去适配Android6.0和Anroid7.0,Android6.0加入了运行时权限申请,Android主要对SD卡的读写做了更加严格的要求,所以本来简单的调用相机和从相册选择一张图片的逻辑便的稍微复杂了那么一点。

    言归正传,我们先来谈谈如何调用相机吧。

     调用相机

        //调用getExternalCacheDir()可以得到应用关联缓存目录,
        // Android6.0之后读写SD卡被列为了危险权限,而这个目录不需要申请权限,
        File temp = new File(getContext().getExternalCacheDir(),System.currentTimeMillis() + ".jpg");
        try {
            if (temp.exists()) {
                temp.delete();
            }
            temp.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Uri imageFileUri = null;
        if (Build.VERSION.SDK_INT >= 24) {
             //如果是7.0 以上的版本就必须使用这个方法,
            // 第一个参数是context,第二个参数可以是任意唯一的字符窜,
            // 第三参数是我们刚刚建立的file对象。
            //因为7.0以后直接使用本地真实路径的Uri本认为是不安全的,会抛出FileUriExposedException异常的
            //而FileProvider这是一种特殊的内容提供器,它使用了和内容提供器类似的机制来对数据进行保护,
            // 可以选择性的将封装过的Uri共享给外部,从而提高了应用的安全性。
            imageFileUri = FileProvider.getUriForFile(getContext(),"com.example.cameratest.fileprovider",temp);
        } else {
             imageFileUri = Uri.fromFile(temp);//获取文件的Uri
        }
        Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//跳转到相机Activity
        it.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);//告诉相机拍摄完毕输出图片到指定的Uri
       startActivityForResult(it, 102);
 然后在onActivityResult()方法中去把这张照片转换为bitmap格式的就OK了

       Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
 当然我们刚刚用到了FileProvider,我们需要为其注册一下

           <provider
            android:authorities="com.example.cameratest.fileprovider"
            android:name="android.support.v4.content.FileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
             <meta-data
                 android:name="android.support.FILE_PROVIDER_PATHS"
                 android:resource="@xml/file_paths"/>
            </provider>

其中android:name的值是固定的,android:authorities属性值必须与FileProvider.getUriForFile()中第二个参数写的一致,另外还需要使用<meta-data>标签来指定Uri的共享路径,并引用一个@xml/file_path资源,当然这个需要我们创建

    

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path=""/>
</paths>
其中external-path用来制定URI共享的,name可以随便填,path的值表示共享的具体路径,空值代表将整个SD卡共享,当然也可以只共享test.jpg这张图片的路径。

另外一点需要注意,在Android4.4之前,访问SD卡的应用关联目录也是需要声明权限的,4.4系统之后不需要再声明,为了兼容,我们最好声明一下SD卡的访问权限。

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

相册中选择图片

我们需要动态申请WRITE_EXTERNAL_STORGAGE这个权限,打开相册去选择图片,打开相册很简单,不再写代码了,我们主要讲一下如何处理返回的这张图片。因为Android4.4之后,选取相册中的图片不再返回图片真实额Uri了,而是一个封装过的Uri,所以我们需要对这个Uri进行解析。下面是4.4以上系统处理图片的代码。
/**
     * 4.4以上系统使用这个方法处理图片
     * @param data
     */
    public handleImageOnKitKat(Intent data) {
        String imagePath = null;
        Uri uri = data.getData();
        if (DocumentsContract.isDocumentUri(this,uri)){
            //如果是document类型的uri,则通过document id来处理
            String docId = DocumentsContract.getDocumentId(uri);
            if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
                String id = docId.split(":")[1];
                String selection = MediaStore.Images.Media._ID + "="+id;
                imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection);
            } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
                Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://download/public_downloads"),Long.valueOf(docId));
                imagePath = getImagePath(contentUri,null);
            }
        } else if ("content".equalsIgnoreCase(uri.getScheme())) {
            //如果是content类型的Uri,则使用普通的方式处理
            imagePath = getImagePath(uri,null);
        } else if ("file".equalsIgnoreCase(uri.getScheme())){
            //如果是file类型的Uri,则直接获取图片路径即可
            imagePath = uri.getPath();
        }
        //根据图片路径去显示图片
        ....
    }

    private String getImagePath(Uri uri,String selection){
        String path = null;
        Cursor cursor = getContext().getContentResolver().query(uri,null,selection,null,null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            }
            cursor.close();
        }
        return path;
    }
而4.4之前就比较简单的,它的Uri并没有被封装过,不需要任何解析。

    private void handleImageBeforeKitKat(Intent data) {
        Uri uri = data.getData();
        String imagePath = getImagePath(uri,null);
        //根据图片路径去显示图片
        ....
    }
下面是压缩图片代码,我觉得还是不错的,有需要的大家可以借鉴一下

 public static Bitmap getBitMapByUri(String path, String outPath) {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        opts.inPreferredConfig = Bitmap.Config.RGB_565;
        Bitmap bitmap = BitmapFactory.decodeFile(path, opts);

        int w = opts.outWidth;
        int h = opts.outHeight;
        // 现在主流手机比较多是800*480分辨率
        float hh = 800f;// 这里设置高度800f
        float ww = 480f;// 这里设置宽度480f
        // 缩放比由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
        // 计算缩放比
        int be = 1;
        if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放
            be = Math.round((opts.outWidth / hh));
        } else if (w < h && h > hh) {// 如果高度高的话根据高度固定大小缩放
            be = Math.round((opts.outHeight / hh));
        }

        if (be <= 0) {
            be = 1;
        }
        opts.inSampleSize = be;
        opts.inJustDecodeBounds = false;

        bitmap = BitmapFactory.decodeFile(path, opts);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);//
        // 质量压缩方法,这100表示不压缩,把压缩后的数据存放到baos中
        int option = 100;

        if (baos.toByteArray().length / 1024 > 100) { // 按照比例缩放完毕后,判断图片质量任然大于100KB,再次进行缩放?
            while (baos.toByteArray().length / 1024 > 100) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
                baos.reset();// 重置baos即清空baos
                bitmap.compress(Bitmap.CompressFormat.JPEG, option, baos);// 这里压缩options%,把压缩后的数据存放到baos中
                option -= 10;// 每次都减去10
            }
            /*ByteArrayInputStream isBm = new ByteArrayInputStream(
					baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
			// ;// 把ByteArrayInputStream数据生成图片
			bitmap = BitmapFactory.decodeStream(isBm, null, null);*/
            FileOutputStream fos = null;
            try {
                if (!FileUtils.fileIsExists("")) {
                    FileUtils.createSDDir("photo");
                }
                File file = new File(outPath);
                if (file.exists()) {
                    file.delete();
                }
                fos = new FileOutputStream(file);
                fos.write(baos.toByteArray());
                fos.flush();
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bitmap;
    }


这些知识都是平常项目中很常用的,大家可以看看,如果有不懂的可以给我留言哈,今天就总结到这里吧,

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值