先设置权限
SimpleDraweeView布局
先定义三个常量
protected static final int CHOOSE_PICTURE = 0;
protected static final int TAKE_PICTURE = 1;
private static final int CROP_SMALL_PICTURE = 2;
做一个弹框来显示以上两个功能
showChoosePicDialog(view);
/**显示修改头像的对话框 **/
public void showChoosePicDialog(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("设置头像");
String[] items = { "选择本地照片", "拍照" };
builder.setNegativeButton("取消", null);
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case CHOOSE_PICTURE: // 选择本地照片
Intent openAlbumIntent = new Intent(
Intent.ACTION_GET_CONTENT);
openAlbumIntent.setType("image/*");
startActivityForResult(openAlbumIntent, CHOOSE_PICTURE);
break;
case TAKE_PICTURE: // 拍照
takePicture();//动态代理
break;
}
}
});
builder.create().show();
}
动态代理
private void takePicture() {
String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (Build.VERSION.SDK_INT >= 23) {
// 需要申请动态权限
int check = ContextCompat.checkSelfPermission(this, permissions[0]);
// 权限是否已经 授权 GRANTED---授权 DINIED---拒绝
if (check != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
}
Intent openCameraIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment
.getExternalStorageDirectory(), "image.jpg");
//判断是否是AndroidN以及更高的版本
if (Build.VERSION.SDK_INT >= 24) {
openCameraIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
tempUri = FileProvider.getUriForFile(Main2Activity.this, "suxi.recviden.com.qiaoxirun_20181220", file);
} else {
tempUri = Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), "image.jpg"));
}
// 指定照片保存路径(SD卡),image.jpg为一个临时文件,每次拍照后这个图片都会被替换
openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);
startActivityForResult(openCameraIntent, TAKE_PICTURE);
}
动态权限判断操作
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
// 没有获取 到权限,从新请求,或者关闭app
Toast.makeText(this, "需要存储权限", Toast.LENGTH_SHORT).show();
}
}
通过onStartActivityForResult执行裁剪操作
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) { // 如果返回码是可以用的
switch (requestCode) {
case TAKE_PICTURE:
startPhotoZoom(tempUri); // 开始对图片进行裁剪处理
break;
case CHOOSE_PICTURE:
startPhotoZoom(data.getData()); // 开始对图片进行裁剪处理
break;
case CROP_SMALL_PICTURE:
if (data != null) {
setImageToView(data); // 让刚才选择裁剪得到的图片显示在界面上
}
break;
}
}
}
裁剪部分
/**
* 裁剪图片方法实现
*
* @param uri
*/
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.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
startActivityForResult(intent, CROP_SMALL_PICTURE);
}
将裁剪图片临时保存
/**
* 保存裁剪之后的图片数据
*
* @param
*/
protected void setImageToView(Intent data) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
photo = FrescoUtils.toRoundBitmap(photo); // 这个时候的图片已经被处理成圆形的了
headPhoto.setImageBitmap(photo);
uploadPic(photo);
}
}
上传到图片界面
private void uploadPic(Bitmap bitmap) {
String imagePath = FrescoUtils.savePhoto(bitmap, Environment
.getExternalStorageDirectory().getAbsolutePath(), String
.valueOf(System.currentTimeMillis()));
Log.e("imagePath", imagePath+"");
if(imagePath != null){
// 拿着imagePath上传了
Log.d("TAG","imagePath:"+imagePath);
}
}