Lakshman Rao Pilaka
Lakshman Rao Pilaka

Reputation: 79

Get path of Image clicked by Android Camera

I need to click an image using the default camera app on an android device and get the path of the image just clicked. I have taken help from this post

stackoverflow forum link

And my code is as follows

public void MarkIn(View view) {

    String fileName = "temp.jpg";
    final int CAPTURE_PICTURE_INTENT = 1;
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, fileName);
    mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
    startActivityForResult(intent, CAPTURE_PICTURE_INTENT);

    String imagePath = capturedImageFilePath;

// .....some code.....

}


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = null;
            cursor = getApplicationContext().getContentResolver().query(mCapturedImageURI, projection, null, null, null);
            int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            capturedImageFilePath = cursor.getString(column_index_data);
        }
    }

The system isn't waiting for Activity to get completed. As my ...some code... in the above snippet is depenedent on the file path, I am getting null pointer exception.

How to make the code execution wait till the activity is completed.

Upvotes: 0

Views: 1066

Answers (2)

Aamir
Aamir

Reputation: 51

This is Probably as you are using sdk version 19 or above.Android has changed the way we access data from KitKat.

Upvotes: 0

Barun
Barun

Reputation: 312

You should first get the image bitmap on image click and then convert that bitmap into uri using this code.:- public Uri getImageUri(Context inContext, Bitmap inImage) {

  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
  String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
  return Uri.parse(path);
}

try this and let me know.

Upvotes: 1

Related Questions