老师,我按照您视频里的代码写的拍照和从相册选择图片,但是在onActivityResult函数中,拍照的话拿到的resultCode是0,图片显示不出来;相册选择图片的话resultCode是-1,但是选出的图片显示到ImageView上是纯白的
用的是模拟器(Pixel 2XL API 29)
下图是从相册选择完图片返回的显示:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
LogUtils.i("resultCode:" + resultCode);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == FileHelper.CAMERA_CODE) {
uploadFile = FileHelper.getInstance().getTempFile();
} else if (requestCode == FileHelper.ALBUM_CODE) {
Uri uri = data.getData();
if (uri != null) {
String path = FileHelper.getInstance().getRealPathFromUri(this, uri);
if (!TextUtils.isEmpty(path)) {
uploadFile = new File(path);
}
}
}
//设置头像
if (uploadFile != null) {
Bitmap mBitmap = BitmapFactory.decodeFile(uploadFile.getPath());
iv_photo.setImageBitmap(mBitmap);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
public void toCamera(Activity mActivity){
try {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String fileName = simpleDateFormat.format(new Date());
tempFile = new File(Environment.getExternalStorageDirectory(), fileName + ".jpg");
//兼容Android N
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
imageUri = Uri.fromFile(tempFile);
} else {
//利用FileProvider
imageUri = FileProvider.getUriForFile(mActivity,
mActivity.getPackageName() + ".fileprovider", tempFile);
//添加权限
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
mActivity.startActivityForResult(intent, CAMERA_CODE);
} catch (Exception e) {
Toast.makeText(mActivity, "无法打开相机", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
public void toAlbum(Activity activity){
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
activity.startActivityForResult(intent, ALBUM_CODE);
}