调用相机

                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 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 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,代表不明确,实际为正常

更多相关文章

  1. Android(安卓)studio 使用问题
  2. Android官方教程翻译(1)——创建第一个Android应用
  3. android 使用命令模拟点击 滑动
  4. android 使用include 调用内部组件
  5. Android(安卓)SQLite数据库解析并使用两种方法实现增删改查
  6. Calling startActivity() from outside of an Activity context
  7. 箭头函数的基础使用
  8. NPM 和webpack 的基础使用
  9. Python list sort方法的具体使用

随机推荐

  1. MySQL中int最大值深入讲解
  2. Mysql主键和唯一键的区别点总结
  3. MySQL按时间统计数据的方法总结
  4. 实例讲解MySQL中乐观锁和悲观锁
  5. SQL和NoSQL之间的区别总结
  6. mysql蠕虫复制基础知识点
  7. Mysql数据表中的蠕虫复制使用方法
  8. MySQL limit性能分析与优化
  9. MySQL和Redis实现二级缓存的方法详解
  10. MySQL普通索引和唯一索引的深入讲解