当我们点击头像想要进行头像上传时,我们可以给上传头像设置一个弹框效果,用来给用户提供相册选择和拍照两种选择。
这时我们可以使用PopupWindow实现
1. 定义图片存储位置
public void saveImage(Bitmap bitmap) {
String state = Environment.getExternalStorageState();
if (!state.equals(Environment.MEDIA_MOUNTED)){
return;
}
FileOutputStream stream = null;
try {
stream = new FileOutputStream(Environment.getExternalStorageDirectory() + "/hear.jpg");
bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);
stream.flush();
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
2. 打开相册
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
startActivityForResult(photoPickerIntent, ALBUM_REQUEST_CODE);
3. 打开系统相机
private void getPicFromCreame() {
//先初始化出相机拍完照片后的存放路径
tempFile = new File(Environment.getExternalStorageDirectory().getPath(), System.currentTimeMillis() + ".jpg");
//跳转到调用系统相机(跳转系统相机用到的是intent隐式意图来进行跳转)
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//跳转前需要对当前的Android版本进行判断,因为Android6.0以上的版本需要权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
builder.detectFileUriExposure();
}
} else {
//否则使用Uri.fromFile(file)方法获取Uri
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
}
startActivityForResult(intent, CAMEAR_REQUEST_CODE);
}
4. 裁剪
public void cropPhoto(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
startActivityForResult(intent, 3);
}
5. 在框架上进行上传
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent intent) {
// super.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode) {
case CAMEAR_REQUEST_CODE:
//调用系统相机后返回
if (resultCode == RESULT_OK) {
//用相机返回的照片去调用剪裁也需要对Uri进行处理
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri contentUri = FileProvider.getUriForFile(this, "com.bw.movie", tempFile);
cropPhoto(contentUri);
} else {
cropPhoto(Uri.fromFile(tempFile));
}
}
break;
case ALBUM_REQUEST_CODE:
//调用系统相册后返回
if (resultCode == RESULT_OK) {
Uri uri = intent.getData();
cropPhoto(uri);
}
break;
case CROP_REQUEST_CODE:
Bundle bundle = intent.getExtras();
if (bundle != null) {
//在这里获得了剪裁后的Bitmap对象,可以用于上传
Bitmap image = bundle.getParcelable("data");
//设置到ImageView上
pic.setImageBitmap(image);
Log.e("TAG","Bit=="+image.toString());
//也可以进行一些保存、压缩等操作后上传
saveImage(image);
File file = new File(Environment.getExternalStorageDirectory() + "/hear.jpg");
getPresenter().headpic(file);
}
break;
}
}
总结:
当我们进行拍照时,有些模拟器可能会因为版本太高从而导致无法打开摄像头,这时问题出在Android7.0权限处理问题,我们可以进行权限添加,点击—>ヾ(•ω•`)o