I'm trying to launch a camera activity and then capture the photo when the user saves it and return to the app. This works fine on my phone running Android 2.2 but I get a NullPointerException on my tablet running 4.1.2 or something.
I see the app is getting a NullPointerException in 'onActivityResult' when I try to make a bitmap out of the 'data' that's passed (clearly no data is getting passed):
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
pauseTime = System.currentTimeMillis();
Log.i(TAG, "Camera result time pauseTime should be reset: " + pauseTime);
Log.i(TAG, "Camera resultCode is equal to: " + resultCode + " RESULT_OK is: " + RESULT_OK);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
try {
Bitmap photo = (Bitmap) data.getExtras().get("data");
} catch (Exception e) {
// Data is null getting sent back
e.printStackTrace();
}
The error in logcat looks like this:
07-29 17:16:50.795: W/System.err(19517): java.lang.NullPointerException
07-29 17:16:50.795: W/System.err(19517): at com.android.thoughtfi.UsersThoughts.onActivityResult(UsersThoughts.java:163)
07-29 17:16:50.795: W/System.err(19517): at android.app.Activity.dispatchActivityResult(Activity.java:5391)
07-29 17:16:50.795: W/System.err(19517): at android.app.ActivityThread.deliverResults(ActivityThread.java:3215)
07-29 17:16:50.795: W/System.err(19517): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3262)
07-29 17:16:50.795: W/System.err(19517): at android.app.ActivityThread.access$1200(ActivityThread.java:140)
07-29 17:16:50.795: W/System.err(19517): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
07-29 17:16:50.800: W/System.err(19517): at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 17:16:50.800: W/System.err(19517): at android.os.Looper.loop(Looper.java:137)
07-29 17:16:50.800: W/System.err(19517): at android.app.ActivityThread.main(ActivityThread.java:4947)
07-29 17:16:50.800: W/System.err(19517): at java.lang.reflect.Method.invokeNative(Native Method)
Why isn't any data getting return to the application's onactivityresult?
EDIT (for extra info, here is the photoButton):
photoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// possible option
String fileName = "temp.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
mCapturedImageURI = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(intent, CAMERA_REQUEST);
}
});
EDIT This seems to work ok, but I'm still getting the photo displayed in landscape mode no matter how I take it on the tablet:
onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
pauseTime = System.currentTimeMillis();
Log.i(TAG, "Camera result time pauseTime should be reset: " + pauseTime);
Log.i(TAG, "Camera resultCode is equal to: " + resultCode + " ");
Log.i(TAG, "The intent mCapturedImageURI was: " + mCapturedImageURI);
Bitmap photo = null;
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
if(data != null){
photo = (Bitmap) data.getExtras().get("data");
} else {
try {
photo = MediaStore.Images.Media.getBitmap(this.getContentResolver(), mCapturedImageURI);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
imageView.setImageBitmap(photo);
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(mCapturedImageURI, projection, null,
null, null);
int column_index_data = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
capturedImageFilePath = cursor.getString(column_index_data);
Photo BUtton:
photoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// possible option
String fileName = "temp.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
mCapturedImageURI = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(intent, CAMERA_REQUEST);
}
});