I'm trying to capture image and save it in gallery. For that in onCreate my code
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
imageUri = Uri.fromFile(photo);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PICTURE);
and
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == TAKE_PICTURE) {
Log.v("data", "data: " + data);
Bundle extras = data.getExtras();
if (extras.containsKey("data")) {
bitmap = (Bitmap) extras.get("data");
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
// bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
// byte[] image = baos.toByteArray();
if (bitmap != null) {
// BitmapFactory.Options options = new
// BitmapFactory.Options();
// options.inSampleSize = 5;
// Bitmap myImage = BitmapFactory.decodeByteArray(image, 0,
// image.length, options);
imageView.setImageBitmap(bitmap);
}
} else {
Toast.makeText(getBaseContext(), "Please capture again", Toast.LENGTH_LONG).show();
}
showPopUpForUpload();
}
}
Now I'm able to capture image, but after that I'm getting error
12-17 15:28:56.090: E/AndroidRuntime(4684): FATAL EXCEPTION: main
12-17 15:28:56.090: E/AndroidRuntime(4684): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=null} to activity {com.example.fashiongirl/com.example.fashiongirl.HomeActivity}: java.lang.NullPointerException
12-17 15:28:56.090: E/AndroidRuntime(4684): at android.app.ActivityThread.deliverResults(ActivityThread.java:2744)
12-17 15:28:56.090: E/AndroidRuntime(4684): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2787)
12-17 15:28:56.090: E/AndroidRuntime(4684): at android.app.ActivityThread.access$2000(ActivityThread.java:122)
12-17 15:28:56.090: E/AndroidRuntime(4684): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1032)
12-17 15:28:56.090: E/AndroidRuntime(4684): at android.os.Handler.dispatchMessage(Handler.java:99)
12-17 15:28:56.090: E/AndroidRuntime(4684): at android.os.Looper.loop(Looper.java:132)
12-17 15:28:56.090: E/AndroidRuntime(4684): at android.app.ActivityThread.main(ActivityThread.java:4025)
12-17 15:28:56.090: E/AndroidRuntime(4684): at java.lang.reflect.Method.invokeNative(Native Method)
12-17 15:28:56.090: E/AndroidRuntime(4684): at java.lang.reflect.Method.invoke(Method.java:491)
12-17 15:28:56.090: E/AndroidRuntime(4684): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
12-17 15:28:56.090: E/AndroidRuntime(4684): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
12-17 15:28:56.090: E/AndroidRuntime(4684): at dalvik.system.NativeStart.main(Native Method)
12-17 15:28:56.090: E/AndroidRuntime(4684): Caused by: java.lang.NullPointerException
12-17 15:28:56.090: E/AndroidRuntime(4684): at com.example.fashiongirl.HomeActivity.onActivityResult(HomeActivity.java:47)
12-17 15:28:56.090: E/AndroidRuntime(4684): at android.app.Activity.dispatchActivityResult(Activity.java:4541)
12-17 15:28:56.090: E/AndroidRuntime(4684): at android.app.ActivityThread.deliverResults(ActivityThread.java:2740)
12-17 15:28:56.090: E/AndroidRuntime(4684): ... 11 more
The null pointer exception is on line
Bundle extras = data.getExtras();
So what is the problem?
I think its a problem of Android version 4.0 +.
Just try to remove,
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
imageUri = Uri.fromFile(photo);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
When you call Capture intent. Now your resulted bundle onActivityResult()
is not NULL
.
Actually, When you are giving MediaStore.EXTRA_OUTPUT
parameter to Camera Intent then camera application doesn't callback with bundled data. (I find this issues in Android 4.0 +)
You have to get the Image file from Uri which you set as MediaStore.EXTRA_OUTPUT
parameter.
Update:
Or use below pathFromUri()
method and pass Uri which you are set to camera intent and get the Real File Path, now for this you don't have to use Bundle extras = data.getExtras();
private String pathFromUri(Uri imageUri) {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(imageUri, filePathColumn,
null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
return filePath ;
}