Android 7.0拍照和裁剪
2017-11-09 本文已影响69人
Android师哥
night_rain.png
在项目中遇到了7.0以上系统拍照和裁剪时候的问题,查询了一下网上大牛写的文章,最后整理一下,做个Demo;
在7.0以上的系统中,拍照和裁剪方面遇到的问题主要的就是Uri转换的问题,之前一直Uri.fromFile()就Ok了,但是在7.0以上的android系统中是不可的,因为它直接暴露的文件的真实路劲!接下来直接上代码;
-
第一步(清单文件中)
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="android.zhen.keng"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_path" />
</provider>
下面是file_path(在res下创建xml目录)
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="images" path="test/"/>
</resources>
-
第二步(拍照)
private void comrea() {
Intent mIntent = new Intent();
mIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/test");
if(!file.exists()){
file.mkdirs();
}
comeraFiel=new File(file ,System.currentTimeMillis() + ".jpg");
Uri uri = null;
//判断SDK是否是6.0以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Log.i("------", "好手机: ");
//是否赋予了权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
mIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//7.0以上需要处理路劲,不可以暴露真实路劲
uri = FileProvider.getUriForFile(this, "android.zhen.keng", comeraFiel);
}else {
uri = Uri.fromFile(comeraFiel);
}
} else {
//重新赋予权限
requestPermissions(new String[]{Manifest.permission.CAMERA}, 1);
}
} else {
//重新赋予权限
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
} else {
Log.i("------", "旧手机: ");
//6.0以下直接转换Uri
uri = Uri.fromFile(comeraFiel);
}
mIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(mIntent, 100);
}
-
第三步(拍照回调)
if (requestCode == 100 && resultCode == RESULT_OK) {
Picasso.with(MainActivity.this)
.load(comeraFiel)
.error(R.color.colorPrimary)
.placeholder(R.color.colorAccent)
.fit()
.config(Bitmap.Config.RGB_565)
.into(iv_mian_1);
cut();
}
-
第四步(裁剪)
private void cut() {
Intent intent = new Intent("com.android.camera.action.CROP");
Uri uri=null;
//裁剪的时候也同样需要
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
uri = FileProvider.getUriForFile(this, "android.zhen.keng", comeraFiel);
}else {
uri=Uri.fromFile(comeraFiel);
}
//用来存放裁剪后的图片
String pgth = Environment.getExternalStorageDirectory().getAbsolutePath();
cutFile = new File(pgth, System.currentTimeMillis() + ".jpg");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 480);
intent.putExtra("outputY", 480);
intent.putExtra("scale", true);
//这个地方不需要转换Uri
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cutFile));
intent.putExtra("return-data", false);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true); // no face detection
startActivityForResult(intent, 300);
}
-
第五步(裁剪回调)
if (requestCode == 300 && resultCode == RESULT_OK) {
Picasso.with(MainActivity.this)
.load(cutFile)
.error(R.color.colorPrimary)
.placeholder(R.color.colorAccent)
.fit()
.config(Bitmap.Config.RGB_565)
.into(iv_mian_2);
}
结束,抽时间做的Demo,不足之处还请见谅!