Hi so i want to take a picture with camera intent but after taking the picture i get the following error:
ERROR:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {radautiul_civic.e_radauti/radautiul_civic.e_radauti.Civic_Alert}: 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:5004)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5047)
at android.app.ActivityThread.access$1600(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7331)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
at radautiul_civic.e_radauti.Civic_Alert.onActivityResult(Civic_Alert.java:86)
at android.app.Activity.dispatchActivityResult(Activity.java:7165)
at android.app.ActivityThread.deliverResults(ActivityThread.java:5000)
Here is my code:
static final int REQUEST_TAKE_PHOTO = 1;
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
And here is my StartActivityForResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
imgBitmap = (Bitmap) data.getExtras().get("data");
img.setImageBitmap(imgBitmap);
Toast.makeText(getApplicationContext(),mCurrentPhotoPath,Toast.LENGTH_SHORT);
}
}
So this is what I did before error: 1.Opened my camera intent 2.Took the picture 3.I pressed OK and i got that the data from result is empty error
I followed this documentation from google: https://developer.android.com/training/camera/photobasics.html#TaskGallery So can you guys tell me what i did wrong? Thanks in advance
If you pass the extra parameter MediaStore.EXTRA_OUTPUT with the camera intent then camera activity will write the captured image to that path and it will not return the bitmap in the onActivityResult
method.
If you will check the path which you are passing then you will know that actual camera had written the captured file in that path.
So you need some changes in your code
To start the camera activity use
Intent cameraIntent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
After capturing the image you will get captured image in the bitmap format in onActivityResult method. Now when you get the bitmap use it as you want to.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
Bitmap bmp = intent.getExtras().get("data");
//Use the bitmap as you want to
}
}
Note: Here bitmap object consists of thumb image, it will not have a full resolution image
Some useful references
http://dharmendra4android.blogspot.in/2012/04/save-captured-image-to-applications.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.