I have searched a lot to solve this issue, but couldnt get it fixed.
I am using a camera function in my app and having troubles passing the intent to the callback method. The user can either upload an image from the gallery
retrieveFromGallery()
which works fine. He also can take a picture
takePicture()
which causes a crash in
onActivityResult()
as the intent is null. It is not null however, when I call
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
The picture gets saved in the file path defined, but I want to display it in the app, as I do when I retrieve it from the gallery. I followed the Android Developer Tutorial, so my code should be the same as there. Only difference: Im using fragments Any help is greatly appreciated :)
private static final int PICK_IMAGE_REQUEST = 2;
private static final int REQUEST_TAKE_PHOTO = 3;
public void retrieveFromGallery() {
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
public void takePicture() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(this.getActivity().getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
Log.d(LOG_TAG, "creating photofile: " + photoFile);
} catch (IOException ex) {
// Error occurred while creating the File
Log.d(LOG_TAG, "error creating the file" + ex);
}
// Continue only if the File was successfully created
if (photoFile != null) {
Log.d(LOG_TAG, "temporary photofile path: " + photoFile);
takePictureIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri photoURI = FileProvider.getUriForFile(this.getActivity(),
getActivity().getApplicationContext().getPackageName() + ".fileprovider", photoFile);
Log.d(LOG_TAG, "uri: " + photoURI);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
Log.d(LOG_TAG, "intent: " + takePictureIntent + " , " + REQUEST_TAKE_PHOTO);
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getActivity().getContentResolver(), uri);
popupWindowSelectUploadSource.dismiss();
selectedImage.setVisibility(View.VISIBLE);
selectedImage.setImageBitmap(bitmap);
Log.d(LOG_TAG, "img path" + bitmap);
} catch (IOException e) {
e.printStackTrace();
}
} else if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
Log.d(LOG_TAG, "on activity result - intent: " + requestCode + "resultcode "+ resultCode + "data " + data);
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
popupWindowSelectUploadSource.dismiss();
selectedImage.setImageBitmap(imageBitmap);
}
}
From my Logcat:
03-18 13:26:36.752 593-593/com.example.myapp D/SpotMap: opening a popup window
03-18 13:26:37.721 593-593/com.example.myapp D/SpotMap: opening the popup window to select upload source
03-18 13:26:38.626 593-593/com.example.myapp D/SpotMap: creating photofile:/storage/emulated/0/Android/data/com.example.myapp/files/Pictures/JPEG_20180318_132638_5713532469653547292.jpg
03-18 13:26:38.627 593-593/com.example.myapp D/SpotMap: temporary photofile: path: /storage/emulated/0/Android/data/com.example.myapp/files/Pictures/JPEG_20180318_132638_5713532469653547292.jpg
03-18 13:26:38.627 593-593/com.example.myapp D/SpotMap: uri: content://com.example.myapp.fileprovider/my_images/JPEG_20180318_132638_5713532469653547292.jpg
03-18 13:26:38.681 593-593/com.example.myapp D/SpotMap: intent: Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 clip={text/uri-list U:content://com.example.myapp.fileprovider/my_images/JPEG_20180318_132638_5713532469653547292.jpg} (has extras) } , 3
03-18 13:26:38.714 593-593/com.example.myapp D/SpotMap: on Pause
03-18 13:26:47.444 593-593/com.example.myapp D/SpotMap: on activity result - intent: 3resultcode -1data null
My error message:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapp, PID: 593
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65539, result=-1, data=null} to activity {com.example.vreeni.StreetMovementApp/com.example.myapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:4339)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4382)
at android.app.ActivityThread.-wrap19(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1654)
UPDATE:
I tried what was suggested in a similar post, I am able to enter into the OnActivityResult method, and just create the URI there. But then, however, it cannot be converted into Bitmap. Returns null.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getActivity().getContentResolver(), uri);
popupWindowSelectUploadSource.dismiss();
selectedImage.setVisibility(View.VISIBLE);
selectedImage.setImageBitmap(bitmap);
Log.d(LOG_TAG, "img path" + bitmap);
} catch (IOException e) {
e.printStackTrace();
}
} else if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
File photoFile = null;
try {
photoFile = createImageFile();
Log.d(LOG_TAG, "creating photofile: " + photoFile);
} catch (IOException ex) {
// Error occurred while creating the File
Log.d(LOG_TAG, "error creating the file" + ex);
}
if (photoFile != null) {
Log.d(LOG_TAG, "temporary photofile path: " + photoFile);
Uri photoURI = FileProvider.getUriForFile(this.getActivity(),
getActivity().getApplicationContext().getPackageName() + ".fileprovider", photoFile);
Log.d(LOG_TAG, "uri: " + photoURI);
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getActivity().getContentResolver(), photoURI);
Log.d(LOG_TAG, "bitmap: " + bitmap);
selectedImage.setVisibility(View.VISIBLE);
selectedImage.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}