我试图在使用相机拍摄后将图像设置为图像视图上的个人资料图片。图像捕获工作正常,图像存储在设备内存中。但是 onActivityResult 代码意图对象变为空。
这是调用相机动作的代码
Intent camera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Uri file = Uri.fromFile(Utilities.getOutputMediaFile());
camera.putExtra(MediaStore.EXTRA_OUTPUT, file);
activity.startActivityForResult(camera, Constants.IMAGE_CAPTURE_CAMERA);
从对话和活动中调用
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Constants.LOGIN_REQUEST) {
if (AppSettings.getInstance(HomeScreen.this).getBooleanValue(AppKeys.LOGIN_STATUS)) {changeFragment(new Fragment_Account());
}
} else {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frameContainer);
fragment.onActivityResult(requestCode, resultCode, data);
}
}
在片段中
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data != null) {
if (requestCode == Constants.IMAGE_CAPTURE_CAMERA && resultCode == Activity.RESULT_OK) {
ProfilePicUri = (Uri) data.getExtras().get(MediaStore.EXTRA_OUTPUT);
Log.d("ImageUri", "onActivityResult: " + ProfilePicUri);
performCrop();
} else if (requestCode == Constants.IMAGE_CAPTURE_GALLERY) {
if (data.getData() != null) {
ProfilePicUri = data.getData();
performCrop();
}
} else if (requestCode == Constants.IMAGE_CROP) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap newProfilePic = extras.getParcelable("data");
if (newProfilePic != null) {
customer.setProfilePic(Utilities.encodeTobase64(newProfilePic));
profilePic.setImageBitmap(newProfilePic);
Bitmap image = ((BitmapDrawable) profilePic.getDrawable()).getBitmap();
String imageUrls = Utilities.encodeTobase64(image);
String imageString = image.toString();
Log.d("imageUrl : ", imageUrls);
}
}
}
}
}
在活动和片段data
中为空。