1

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 :)

Jay
  • 4,873
  • 7
  • 72
  • 137

1 Answers1

4

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);
bilal
  • 2,187
  • 3
  • 22
  • 23
  • Do you know how to pass this Uri object to another activity? – Jay Sep 02 '15 at 05:39
  • I it won't work then try these answers. http://stackoverflow.com/questions/2174732/can-i-get-uri-of-image-file-from-its-name http://stackoverflow.com/questions/3004713/get-content-uri-from-file-path-in-android – bilal Sep 02 '15 at 05:40
  • Add Uri to extras in intent see [this](http://stackoverflow.com/questions/8017374/how-to-pass-a-uri-to-an-intent) answer – bilal Sep 02 '15 at 05:48
  • Mate, when I try to set the Uri to a imageview, nothing happens. The path is properly printed when I log it however I cant seem to set it to an image view. I tried converting it to a bitmap as well. Can u help out? – Jay Sep 02 '15 at 06:04
  • Beautiful answer! – Ruchir Baronia Nov 21 '17 at 05:10