0

I'm working on an Android project which allow the user to take a picture using the camera.

My method looks like this :

private void takePicture(){
        Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(camera, 1337);
    }

And in my onActivityResult I take the picture and store it in my SdCard. I trying to modify my takePicture() function to return the path of the picture (String instead void).

I already know that getPath() from java.io.File allow me to do that.

So my question is : how can I modify my function to return the path of the picture?

13KZ
  • 1,295
  • 5
  • 24
  • 43
  • Take a look to this post: [handling-onactivityresult](http://stackoverflow.com/questions/7262405/handling-onactivityresult-in-android-app-having-more-than-one-activity) – Damien R. Mar 21 '13 at 14:34
  • 1
    Duplicate of [this question](http://stackoverflow.com/questions/2729267/android-camera-intent), take a look there. – adrianp Mar 21 '13 at 14:37

2 Answers2

1

takePicture won't be able to return a path, as it'll return before your intent has completed. You'll need to do something with the path inside onActivityResult

Rawkode
  • 21,990
  • 5
  • 38
  • 45
1

Here is the code to get the path of captured and stored image in pictureCallback

in this code mFilePath is the string which is holding the path of image...

PictureCallback mPicture = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            File pictureFile = getOutputMediaFile();
            if (pictureFile == null) {
                return;
            }
            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();

                String mFilePath = pictureFile.getAbsolutePath(); 
            } catch (FileNotFoundException e) {

            } catch (IOException e) {
            }
        }
    };

    private static File getOutputMediaFile() {
        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;
            }
        }
        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");

        return mediaFile;
    }
}
Developer
  • 291
  • 2
  • 11
  • He already says that he's storing the image to SD card in `onActivityResult`, so why is this relevant? – Rawkode Mar 21 '13 at 14:47
  • @Rawkode in the above examples there are methods which shows the path he just need to make void method to String type and return the path... – Developer Mar 21 '13 at 14:50
  • @Rawkode whether this answer is useful..?? – Developer Mar 21 '13 at 14:53
  • The question is "How can I modify `takePicture` to return a `String`" that `String` being the path of the image. Which still isn't possible. – Rawkode Mar 21 '13 at 14:59
  • @Rawkode brother you had already mentioned that `takePicture` can not be return the String value so i just showing him another way to getPath,.. if i write in my question the same you write in my words is duplication so i didn't write that and just show him way how to do.. – Developer Mar 21 '13 at 15:02