2

My app takes a picture using the phones camera and stores the picture in the phones gallery. I would like to then get that picture path and store it in my datastore. Can someone please help me? Heres my code:

public void onClick(View v){
        switch(v.getId())
        {
            case R.id.btnLoadPic:

                //Options for the dialogue menu
                final CharSequence[] items = {"Camera", "Gallery"};

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Choose an Option");
                builder.setItems(items, new DialogInterface.OnClickListener() {
                    /**
                     * Make onclick functionality for the options in the dialogue menu
                     */
                    public void onClick(DialogInterface dialog, int item) {

                        // Camera option
                        if (item == 0){

                            PackageManager pm = getPackageManager();

                            if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)){
                                //Toast.makeText(this, "camera", Toast.LENGTH_SHORT).show();
                                dispatchTakePictureIntent(11);
                            } else {
                                Toast.makeText(null, "No camera avalible", Toast.LENGTH_SHORT).show();

                            }
                        }
                        // Gallery option this works fine


    private void dispatchTakePictureIntent(int actionCode) {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(takePictureIntent, actionCode);


            //handleSmallCameraPhoto(takePictureIntent);
        }

        private void handleSmallCameraPhoto(Intent intent) {
            Bundle extras = intent.getExtras();
            Bitmap mImageBitmap = (Bitmap) extras.get("data");
            ImageView mImageView = (ImageView) this.findViewById(R.id.imagePlayer);
            mImageView.setImageBitmap(mImageBitmap);

        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            handleSmallCameraPhoto(data);
        }

The last bits of codes accesses the camera and displays the picture in the imageview. How do i get that picture path in a string format?

Lilu Patel
  • 301
  • 2
  • 8
  • 23
  • Walk through these links you will get the answers http://stackoverflow.com/questions/4184951/get-path-of-image-from-action-image-capture-intent http://stackoverflow.com/questions/9092532/capturing-image-from-gallery-camera-in-android – Daud Arfin Sep 06 '12 at 08:01
  • check this http://stackoverflow.com/questions/11927710/get-image-from-capture-and-show-image-in-another-layout-using-another-activity-i/11927947#11927947 – G_S Sep 06 '12 at 08:13

1 Answers1

9

This will help. Tested and worked!

public class MainActivity extends Activity {

    private static final int REQUEST_IMAGE = 100;   
    private static final String TAG = "MainActivity";

    TextView tvPath;
    ImageView picture;
    File destination;
        String imagePath;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvPath = (TextView) findViewById(R.id.idTvPath);
        picture = (ImageView) findViewById(R.id.idIvImage);
        String name =   dateToString(new Date(),"yyyy-MM-dd-hh-mm-ss");
        destination = new File(Environment.getExternalStorageDirectory(), name + ".jpg");

        Button click = (Button) findViewById(R.id.idBtnTakePicture);
        click.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destination));
                startActivityForResult(intent, REQUEST_IMAGE);

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if( requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK ){
            try {
                FileInputStream in = new FileInputStream(destination);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 10;
                imagePath = destination.getAbsolutePath();
                tvPath.setText(imagePath);
                Bitmap bmp = BitmapFactory.decodeStream(in, null, options);
                picture.setImageBitmap(bmp);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        }
        else{
            tvPath.setText("Request cancelled");
        }
    }

    public String dateToString(Date date, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(date);
    }

}

Dont forget to add these in your manifest file:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
  • I have just tested it on my phone and after it takes a picture and tries to save it, it just force closes :( .. i thought maybe its my coding so i tried the version before which worked when i tried it before the changes and it ddnt work either :( .. now my app asks for hardware permission which it was not doing before. Is it something to do with that? – Lilu Patel Sep 06 '12 at 08:56
  • Could you tell me where it forces closed and what is the exception? Does you device has a built in camera?(if not it wont work) Have you tried the sample above? It worked on all the devices I tested. If your device has not a built in camera, you have to customize and in that case you will need the hardware permission. – Lazy Ninja Sep 06 '12 at 09:37
  • @MrPablo Could you elaborate? You got an exception? – Lazy Ninja Mar 29 '13 at 00:52
  • Cool, with destination.getPath(); we can get path to the last camera taken (and saved) photo and that is what I needed, Thanks. – Ragaisis Oct 16 '14 at 06:20