0

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.

Community
  • 1
  • 1

2 Answers2

1

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.

Barun
  • 312
  • 3
  • 16
  • Hi @Barun. I have used your code to get the image uri and brought the ....some code... to the activity result method rather than calling immediately after camera intent invocation. its working now, thank you. – Lakshman Rao Pilaka Mar 19 '15 at 13:48
0

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

Aamir
  • 21
  • 6