I have a problem using camera. I take a photo and save it on a File. On result from camera i save photo on gallery and inside internal folder. Sometimes i've got an unable to resume activity nullpointer exception.
with this code i open camera:
private void photoButtonClicked() {
if (!runScreen.isAdded()) {
return;
}
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
}
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, 2);
}
}
}
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);
// imageFile = File.createTempFile(imageFileName, /* prefix */
// ".jpg", /* suffix */
// storageDir /* directory */
// );
imageFile = new File(storageDir, imageFileName + ".jpg");
return imageFile;
}
this is the code on result:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2 && resultCode == -1) {
try {
ExifInterface exif = new ExifInterface(
imageFile.getAbsolutePath());
int rotation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
int rotationInDegrees = exifToDegrees(rotation);
} catch (Exception e) {
e.printStackTrace();
}
try {
Bitmap image = decodeSampledBitmapFromResource(
imageFile.getAbsolutePath(), 1280, 800);
FileOutputStream out = new FileOutputStream(
gpsService.getString(R.string.urlSave)
+ gpsService.getPhotoName()
+ gpsService.getString(R.string.jpeg));
image.compress(Bitmap.CompressFormat.JPEG, 30, out);
image.recycle();
image = null;
System.gc();
System.gc();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
galleryAddPic();
}
}
private void galleryAddPic() {
// Intent mediaScanIntent = new Intent(
// Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(imageFile.getAbsolutePath());
// Uri contentUri = Uri.fromFile(f);
// mediaScanIntent.setData(contentUri);
// this.sendBroadcast(mediaScanIntent);
if (new SharedSharedPreferences(this).getSaveAutomatically()) {
ContentValues values = new ContentValues();
values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.MediaColumns.DATA,
imageFile.getAbsolutePath());
getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI,
values);
} else {
f.delete();
}
}
private int exifToDegrees(int exifOrientation) {
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
return 90;
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
return 180;
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
return 270;
}
return 0;
}
public static Bitmap decodeSampledBitmapFromResource(String path,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and
// keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
and this is the error:
06-10 15:57:02.381: E/AndroidRuntime(8443): FATAL EXCEPTION: main
06-10 15:57:02.381: E/AndroidRuntime(8443): java.lang.RuntimeException:
Unable to resume activity {cmd.run/cmd.run.run.specific.RunTabDefault}:
java.lang.NullPointerException
06-10 15:57:02.381: E/AndroidRuntime(8443): at
android.app.ActivityThread.performResumeActivity(ActivityThread.java:2919)
06-10 15:57:02.381: E/AndroidRuntime(8443): at
android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2948)
06-10 15:57:02.381: E/AndroidRuntime(8443): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2354)
06-10 15:57:02.381: E/AndroidRuntime(8443): at
android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3877)
06-10 15:57:02.381: E/AndroidRuntime(8443): at
android.app.ActivityThread.access$800(ActivityThread.java:159)
06-10 15:57:02.381: E/AndroidRuntime(8443): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1322)
06-10 15:57:02.381: E/AndroidRuntime(8443): at
android.os.Handler.dispatchMessage(Handler.java:99)
06-10 15:57:02.381: E/AndroidRuntime(8443): at
android.os.Looper.loop(Looper.java:176)
06-10 15:57:02.381: E/AndroidRuntime(8443): at
android.app.ActivityThread.main(ActivityThread.java:5419)
06-10 15:57:02.381: E/AndroidRuntime(8443): at
java.lang.reflect.Method.invokeNative(Native Method)
06-10 15:57:02.381: E/AndroidRuntime(8443): at
java.lang.reflect.Method.invoke(Method.java:525)
06-10 15:57:02.381: E/AndroidRuntime(8443): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:1046)
06-10 15:57:02.381: E/AndroidRuntime(8443): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
06-10 15:57:02.381: E/AndroidRuntime(8443): at
dalvik.system.NativeStart.main(Native Method)
06-10 15:57:02.381: E/AndroidRuntime(8443): Caused by:
java.lang.NullPointerException
06-10 15:57:02.381: E/AndroidRuntime(8443): at cmd.run.run.specific.RunTabDefault.onResume(RunTabDefault.java:430)
06-10 15:57:02.381: E/AndroidRuntime(8443): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1209)
06-10 15:57:02.381: E/AndroidRuntime(8443): at android.app.Activity.performResume(Activity.java:5450)
06-10 15:57:02.381: E/AndroidRuntime(8443): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2909)
06-10 15:57:02.381: E/AndroidRuntime(8443): ... 13 more
Why don't you use something like this?
To call the camera intent use this on button click:
and in activity that had the canvas use this method:
I hope this will help.