Android Camera: Get Picture Uri after the photo is taken

Dinuka Jay

I need to get the File path Uri of the image after the user takes a photo using SurfaceView in Android. I need this so that I can display the image in another view.

Here's my current code:

captureImage.setOnClickListener(new OnClickListener() {
         @Override
            public void onClick(View v) {
                mCamera.takePicture(null, null, jpegCallback);
         }
});

        mSurfaceView.getHolder().addCallback(this);
        mSurfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        mCamera = Camera.open();


        jpegCallback = new PictureCallback() {
            public void onPictureTaken(byte[] data, Camera camera) {

                try {

                    Intent i = new Intent(CameraActivity.this, ImageEditor.class);
                    //i.putExtra("image", I_need_to_send_the_Uri);
                    startActivityForResult(i, 0);

                } catch (Exception e) {
                     e.printStackTrace();
                }
            }
        };

    }


@Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mCamera.setPreviewDisplay(mSurfaceView.getHolder());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Camera.Parameters params = mCamera.getParameters();
        List<Camera.Size> sizes = params.getSupportedPreviewSizes();
        Camera.Size selected = sizes.get(0);
        params.setPreviewSize(selected.width, selected.height);
        mCamera.setParameters(params);

        mCamera.setDisplayOrientation(90);
        mCamera.startPreview();

    }

How do I get the Image Uri from it? Or what other method do you suggest to open the image in another view? I tried sending the byte[] array and the bitmap but it's way too heavy to be transferred between views and that's bad practice. Help out if you can :)

bilal

First of all you need to save the data in to file and store it in to device storage.

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();
        } 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;
}

Get Uri from file by this code

Uri.fromFile(Filename);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get uri of picture taken by camera

From Dev

Android: Can't get the size of photo taken by camera acitivity

From Dev

Android Camera Preview Stretched in Preview, Not After Picture Taken

From Dev

Custom camera shows dark picture preview when photo taken on Nexus

From Dev

Android adding image to picture taken with camera

From Dev

Strange size of picture taken with Android Camera

From Dev

Android - Get URI from Picture that i just have taken (without save the image again)

From Dev

How to show Picture into a activity after taken and saved from custom camera

From Dev

Android - Saving photo taken from Camera , Out of memory

From Dev

Android - take picture - pass uri to camera app

From Dev

Android - take picture - pass uri to camera app

From Dev

How to set the size and quality of a picture taken with the android camera?

From Dev

Delete picture taken from camera

From Dev

Android - get camera photo orientation not working

From Dev

How to detect if photo was taken (camera intent)

From Dev

Photo taken through camera intent is losing on quality

From Dev

picture taken with camera doesn't resize in uiimageview

From Dev

Upload a picture taken by the camera to a server with limited size

From Dev

Getting the URL of picture taken by camera with Photos Framework

From Dev

Get device orientation for photos taken with Android's default camera

From Dev

Get uri from camera intent in android

From Dev

Can't get photo printed after camera shot

From Dev

Android app using camera, photo disappears after screen rotation

From Dev

Getting a black screen after save photo - Android Camera

From Dev

Android app using camera, photo disappears after screen rotation

From Dev

Android Camera App preview after photo capture issue

From Dev

How to get a picture from the photo library with PhoneGap on Android

From Dev

android: camera is not taking a picture

From Dev

Is it possible to check if an image is taken by camera or uploaded from photo library by JavaScript?

Related Related

  1. 1

    Get uri of picture taken by camera

  2. 2

    Android: Can't get the size of photo taken by camera acitivity

  3. 3

    Android Camera Preview Stretched in Preview, Not After Picture Taken

  4. 4

    Custom camera shows dark picture preview when photo taken on Nexus

  5. 5

    Android adding image to picture taken with camera

  6. 6

    Strange size of picture taken with Android Camera

  7. 7

    Android - Get URI from Picture that i just have taken (without save the image again)

  8. 8

    How to show Picture into a activity after taken and saved from custom camera

  9. 9

    Android - Saving photo taken from Camera , Out of memory

  10. 10

    Android - take picture - pass uri to camera app

  11. 11

    Android - take picture - pass uri to camera app

  12. 12

    How to set the size and quality of a picture taken with the android camera?

  13. 13

    Delete picture taken from camera

  14. 14

    Android - get camera photo orientation not working

  15. 15

    How to detect if photo was taken (camera intent)

  16. 16

    Photo taken through camera intent is losing on quality

  17. 17

    picture taken with camera doesn't resize in uiimageview

  18. 18

    Upload a picture taken by the camera to a server with limited size

  19. 19

    Getting the URL of picture taken by camera with Photos Framework

  20. 20

    Get device orientation for photos taken with Android's default camera

  21. 21

    Get uri from camera intent in android

  22. 22

    Can't get photo printed after camera shot

  23. 23

    Android app using camera, photo disappears after screen rotation

  24. 24

    Getting a black screen after save photo - Android Camera

  25. 25

    Android app using camera, photo disappears after screen rotation

  26. 26

    Android Camera App preview after photo capture issue

  27. 27

    How to get a picture from the photo library with PhoneGap on Android

  28. 28

    android: camera is not taking a picture

  29. 29

    Is it possible to check if an image is taken by camera or uploaded from photo library by JavaScript?

HotTag

Archive