Android 调用相机和调用图库

这篇博客介绍了如何在Android中调用相机和相册,包括ACTION_PICK和ACTION_GET_CONTENT的区别,并探讨了调用系统图片剪裁的实现。此外,还讨论了拍照后图片可能发生的旋转问题及其解决方案,特别是在不同设备上显示不一致的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

调用相机

                Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");
                intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
                startActivityForResult(intent,TAKE_PHOTO);

调用相册两种

Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
// 设置文件类型
intent.setType("image/*");
activity.startActivityForResult(intent, requestCode);

Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
// 设置文件类型
intent.setType("image/*");
activity.startActivityForResult(intent, requestCode);

详细代码请看(解析封装的uri获取真实图片路径那一块还没看懂,有空看看)

https://github.com/18668197127/PhotoAndGallery 

 

 两者的区别

如果你有一些特定的集合(由URI标识)想让用户选择,使用ACTION_PICK
如果让用户基于MIME Type选择数据,使用ACTION_GET_CONTENT
在平局的情况下,建议使用ACTION_GET_CONTENT

 

调用系统的图片剪裁

    //剪裁图片
    protected void startPhotoZoom(Uri uri) {

        if (uri == null) {
            Log.i("tag", "The uri is not exist.");
        }
//        tempUri = uri;
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        intent.setDataAndType(uri, "image/*");
        // 设置裁剪
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 100);
        intent.putExtra("outputY", 100);
        //true直接返回一个Bitmap
        intent.putExtra("return-data", false);
        //设置格式
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        //是否保留图片比例
        intent.putExtra("scale", true);
        File out = new File(getExternalStorageDirectory(),"crop1.jpg");
        if (out.exists()){
            out.delete();
            try {
                out.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (Build.VERSION.SDK_INT>=24){
            cropFinishUri=FileProvider.getUriForFile(MainActivity.this,"fileProvider",out);
            //这段代码不知道什么作用,先放着以后研究
            List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
            Log.i(TAG, "ResolveInfo的Size: "+resInfoList.size());
            for (ResolveInfo resolveInfo : resInfoList) {
                String packageName = resolveInfo.activityInfo.packageName;
                grantUriPermission(packageName, cropFinishUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
            }
        }else {
            cropFinishUri=Uri.fromFile(out);
        }
        Log.i(TAG, "startPhotoZoom: "+cropFinishUri);
        //设置输出到的uri
        intent.putExtra(MediaStore.EXTRA_OUTPUT, cropFinishUri);
        startActivityForResult(intent, CROP_SMALL_PICTURE);
    }

(参考别人的代码,还未自己研究过)

cropFinishUri=FileProvider.getUriForFile(MainActivity.this,"fileProvider",out);使用FileProvider封装过的Uri保存文件时报错,使用cropFinishUri=Uri.fromFile(out);方法可以,不知道为什么

如果使用FileProvider封装的Uri加上以下代码也可以实现

(FileProvider封装的Uri需要用grantUriPermission手动赋予临时权限)

        List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        for (ResolveInfo resolveInfo : resInfoList) {
            String packageName = resolveInfo.activityInfo.packageName;
            grantUriPermission(packageName, cropFinishUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }

获取拍照的图片发生旋转的情况

解决 拍照之后手动调整回来:

Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap bitmap1=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);

获取旋转角度(有待研究)根据获取的旋转度数,再手动旋转回来

(小米8手机自动旋转90度,华为P10手机正常)

String filePath=new File(getExternalStorageDirectory(),"take_photo1.jpg").getPath();
ExifInterface exifInterface = new ExifInterface(filePath);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
Log.i(TAG, "onActivityResult: orientation "+orientation);

这里我的小米手机 orientation 值为6,代表向左旋转90°,华为手机orientation 值为0,代表不明确,实际为正常

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值