Ok so I am a newb to android development and as such will more than
likely have some dumb questions. But anyways here I go. The problem I
am having is with my camera app. The way it works right now is that
when you take a picture it will save it then finish the activity and
launch a new camera intent activity. So the problem is that when you
take a few pictures then press the back button it will not go to the
last application before the camera was launched but instead go to the
last instance of the camera. But anyways, a result of reopening the
last activity instance is that if I take a picture after pressing the
back button, it overwrites the original picture taken by that
activity. I overrode the onKeyDown for the back button to call finish
as a solution to this problem but I don't think that is the correct
fix. If someone could please offer me some insight as how to fix my
problem let me know. My code is provided below:

    package com.camera.test;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Date;

    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.provider.MediaStore;
    import android.provider.MediaStore.Images.Media;
    import android.util.Log;
    import android.view.KeyEvent;
    import android.widget.Toast;

    public class CameraCaptureActivity extends Activity {

        protected boolean _taken = true;
        File sdImageMainDirectory;

        private String dateString;

        protected static final String PHOTO_TAKEN = "photo_taken";
        //protected static final Date start = new Date();

        @Override
        public void onCreate(Bundle savedInstanceState) {
                //Log.i("me", "first ever!");
            try {
                //Log.i("me", "first!");
                super.onCreate(savedInstanceState);
                //Log.i("me", "second!");
                        File root = new File(Environment
                                .getExternalStorageDirectory()
                                + File.separator + "Pics" +
File.separator);
                        Log.i("me", root.getAbsolutePath());
                        root.mkdirs();
                        //Log.i("me", "fourth!");
                        Date d = new Date();
                        dateString = (d.getMonth() + 1) + "_" +
d.getDate() + "_" + d.getHours() + "_" + d.getMinutes() + "_" +
d.getSeconds();
                        sdImageMainDirectory = new File(root, "Name_"
+ dateString + ".jpg");
                        Log.i("me", "oncreate: " +
sdImageMainDirectory.toString());
                        //sdImageMainDirectory.deleteOnExit();


                    startCameraActivity();
                    //Log.i("me", "done");

            } catch (Exception e) {
                finish();
                Toast.makeText(this, "Error occured. Please try again
later.",
                        Toast.LENGTH_SHORT).show();
            }

        }

        protected void startCameraActivity() {

                Log.i("me", "startCamera 1");
                Uri outputFileUri = Uri.fromFile(sdImageMainDirectory);
                //Log.i("me", "startCamera 2");
            Intent intent = new
Intent("android.media.action.IMAGE_CAPTURE");
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            //Log.i("me", "startCamera 3");
            startActivityForResult(intent, 0);

        }

        @Override
        protected void onActivityResult(int requestCode, int
resultCode, Intent data) {
            switch (resultCode) {
            case 0:
                Log.i("me", "on activity case 0");
                finish();
                break;

            case -1:

                try {
                        Log.i("me", "on activity case -1");
                    StoreImage(this, Uri.parse(data.toURI()),
                            sdImageMainDirectory);
                    //Log.i("me", "on activity 3");
                } catch (Exception e) {
                    e.printStackTrace();
                }

                finish();
                startActivity(new Intent(CameraCaptureActivity.this,
this.getClass()));

            }

        }

        @Override
        protected void onRestoreInstanceState(Bundle
savedInstanceState) {
            if
(savedInstanceState.getBoolean(CameraCaptureActivity.PHOTO_TAKEN)) {
                _taken = true;
                Log.i("me", "on Restore 1");
            }
        }

        @Override
        protected void onSaveInstanceState(Bundle outState) {
            outState.putBoolean(CameraCaptureActivity.PHOTO_TAKEN,
_taken);
            Log.i("me", "on save 1");
        }

        public static void StoreImage(Context mContext, Uri imageLoc,
File imageDir) {
            Bitmap bm = null;
            try {
                bm = Media.getBitmap(mContext.getContentResolver(),
imageLoc);
                FileOutputStream out = new FileOutputStream(imageDir);
                Log.i("me", "StoreImage: " + out.toString());
                bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
                bm.recycle();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event)
        {
            if ((keyCode == KeyEvent.KEYCODE_BACK))
            {
                finish();
            }
            return super.onKeyDown(keyCode, event);
        }

    }

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to