Reputation: 2864
I am trying to set a image as profile picture on image view after capture using camera. image capturing is working fine and image is stored in device memory. But on onActivityResult code intent object goes null.
Here is the code for call camera action
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);
which is calling from a dialoge and in Activity
@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);
}
}
In fragment
@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);
}
}
}
}
}
in activity and fragment data
came as null.
Upvotes: 0
Views: 578
Reputation: 1007534
But on onActivityResult code intent object goes null.
It is supposed to be null
. You know where the image should be located: it is where you specified in EXTRA_OUTPUT
. Look there for the image.
Also note that Uri.fromFile()
will not work for you on Android 7.0+, once your targetSdkVersion
climbs to 24 or higher. Please switch to using FileProvider
and its getUriForFile()
method. This sample app from this book illustrates how to do this.
Upvotes: 3
Reputation: 10254
Remove the line where you set the path to save your file unless you need to set your custom path to save your file.
Once an URI is set by
Intent.putExtra(MediaStore.EXTRA_OUTPUT, file);
the resulting intent will be null and it's designed to perform like that.
onActivityResult,
intent.getData()
will give you the URI of the captured image.
Upvotes: 1