Question

I am developing an app to capture an image and save it to sd card ..but everytime it saves current file and removes previous one i have used this example Code Used:

//On button Click

       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

//for result

      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Image captured and saved to fileUri specified in the Intent

            Toast.makeText(this, "Image saved to:\n" + data.getData(),
                    Toast.LENGTH_LONG).show();

        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
        }
    }

//To save file

     private static Uri getOutputMediaFileUri(int type){
   return Uri.fromFile(getOutputMediaFile(type));
     }

     private static File getOutputMediaFile(int type){

File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
          Environment.DIRECTORY_PICTURES), "MyCameraApp");
if (! mediaStorageDir.exists()){
    if (! mediaStorageDir.mkdirs()){
        Log.d("MyCameraApp", "failed to create directory");
        return null;
    }
}


String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date(type));
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
    mediaFile = new File(mediaStorageDir.getPath() + File.separator +
    "IMG_"+ timeStamp + ".jpg");
} else {
    return null;
}

return mediaFile;
 }

Logcat

              02-28 17:12:49.606: E/AndroidRuntime(4791): FATAL EXCEPTION: main
02-28 17:12:49.606: E/AndroidRuntime(4791): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.example.captureimage/com.example.captureimage.MainActivity}: java.lang.NullPointerException
02-28 17:12:49.606: E/AndroidRuntime(4791):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
02-28 17:12:49.606: E/AndroidRuntime(4791):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023)
02-28 17:12:49.606: E/AndroidRuntime(4791):     at android.app.ActivityThread.access$1100(ActivityThread.java:123)
02-28 17:12:49.606: E/AndroidRuntime(4791):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177)
02-28 17:12:49.606: E/AndroidRuntime(4791):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 17:12:49.606: E/AndroidRuntime(4791):     at android.os.Looper.loop(Looper.java:137)
02-28 17:12:49.606: E/AndroidRuntime(4791):     at android.app.ActivityThread.main(ActivityThread.java:4424)
02-28 17:12:49.606: E/AndroidRuntime(4791):     at java.lang.reflect.Method.invokeNative(Native Method)
02-28 17:12:49.606: E/AndroidRuntime(4791):     at java.lang.reflect.Method.invoke(Method.java:511)
02-28 17:12:49.606: E/AndroidRuntime(4791):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-28 17:12:49.606: E/AndroidRuntime(4791):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-28 17:12:49.606: E/AndroidRuntime(4791):     at dalvik.system.NativeStart.main(Native Method)
02-28 17:12:49.606: E/AndroidRuntime(4791): Caused by: java.lang.NullPointerException
02-28 17:12:49.606: E/AndroidRuntime(4791):     at com.example.captureimage.MainActivity.onActivityResult(MainActivity.java:140)
02-28 17:12:49.606: E/AndroidRuntime(4791):     at android.app.Activity.dispatchActivityResult(Activity.java:4676)
02-28 17:12:49.606: E/AndroidRuntime(4791):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)

its on this line:

 Toast.makeText(this, "Image saved to:\n" + data.getData(),
                    Toast.LENGTH_LONG).show();
Was it helpful?

Solution

Your timestamp is always the same. You are overwriting yor previous picture.

You should try:

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());

You must consider also that if you pass intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); to the camera intent. The camera will not return an intent so data.getData(); in onActivityResult will raise a nullPointerException. You should save the path of the output file you specified for later use if needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top