如有不对的地方,望各位小哥哥,小姐姐指导,小女子在此谢过(*^__^*) 嘻嘻……,如果喜欢记得点赞评论哦
一、效果图先贴一贴
说明:
1.拍照用到的相关的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
2.头像显示使用的是facebook的,还么有用过的小伙伴,推荐去看看:
implementation 'com.facebook.fresco:fresco:1.3.0'
二、点击拍照
/**
* 拍照
*/
private void openCamera() {
Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
pictureStr = FileUtil.getPhotopath();
pictureFile = new File(pictureStr);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // 大于等于24即为7.0及以上执行内容
pictureUri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".fileprovider", pictureFile);
} else {
// 低于24即为7.0以下执行内容
pictureUri = Uri.fromFile(pictureFile);
}
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);
camera_intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(camera_intent, AppCode.requestCode.RESULT_CAPTURE);
}
注意:7.0权限,图片的路劲是使用FileProvider不然会抛出异常的,同时在AndroidManifest.mxl要进行相应的配置,这里的.fileprovider,要跟xml里Provider标签下authorities属性包名后的值一样
7.0配置
<!-- Android7.0系统 拍照 -->
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="项目包名.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
在res/values下新建xml,并创建file_paths文件
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="takePicture"
path="." />
</paths>
二、选择相册
/**
* 相册
*/
public void openAblum() {
Intent photo_album_i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(photo_album_i, "请选择图片"), AppCode.requestCode.RESULT_PICK);
}
三、剪裁
/**
* 打开裁剪界面
*/
public void starCropPhoto(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
if (SELECT == 1) {//拍照
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//系統>7.0
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {//系統<7.0
}
intent.putExtra("scale", true);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(pictureFile));
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
} else {//相册
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(pictureFile = new File(FileUtil.getPhotopath())));
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
}
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("noFaceDetection", true);
startActivityForResult(intent, AppCode.requestCode.CROP_PHOTO);
// intent.putExtra("scale", true);
// intent.putExtra("scaleUpIfNeeded", true);
// intent.setDataAndType(uri, "image/*");
// // crop为true是设置在开启的intent中设置显示的view可以剪裁
// intent.putExtra("crop", "true");
// //剪裁比例
// intent.putExtra("aspectX", 1);
// intent.putExtra("aspectY", 1);
// // outputX outputY 是裁剪图片宽高
// intent.putExtra("outputX", 200);
// intent.putExtra("outputY", 200);
// intent.putExtra("noFaceDetection", true);
// startActivityForResult(intent, CROP_PHOTO);
}