-2

Im trying to get the URI for the captured image from camera I why am i getting the exception as java.lang.NullPointerException: uri ,

here is my code.

Uri selectedImage = data.getData();


file = new File(URI(selectedImage)); 

public String URI(Uri contentUri)
 {
        Cursor cursor = null;

try
 {




String[] proj = {MediaStore.ACTION_IMAGE_CAPTURE};

     cursor=getActivity().getContentResolver().query(contentUri,proj,null,null,null);
      intcolumn_index=cursor.getColumnIndexOrThrow(MediaStore.ACTION_IMAGE_CAPTURE);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
Ankita Shah
  • 1,866
  • 16
  • 31
Abilash
  • 59
  • 1
  • 9

1 Answers1

1

You have many problems in this code, even ignoring the fact that it is incomplete. I am going to assume, based on your words, that you are calling startActivity() for ACTION_IMAGE_CAPTURE, and that the code in your question is from onActivityResult().

First, the result from ACTION_IMAGE_CAPTURE does not provide you with a Uri to anything. If you used EXTRA_OUTPUT on your ACTION_IMAGE_CAPTURE Intent, then your image is supposed to be wherever you requested it to be, based on the value you put into EXTRA_OUTPUT. Otherwise, you get back a Bitmap, representing a thumbnail-sized image, from data.getExtra("data").

Second, file = new File(URI(selectedImage)); is completely meaningless, even if selectedImage was not null. AFAIK, that line will not even compile. Even if it did, a Uri is not a File.

Third, MediaStore.ACTION_IMAGE_CAPTURE is not a valid column to retrieve from the MediaStore.

Fourth, you are assuming that the MediaStore knows something about the picture that the camera app took. There is no requirement that the camera app tell the MediaStore about this picture.

FWIW, this sample app demonstrates taking a photo using ACTION_IMAGE_CAPTURE, then using a Uri to that image with ACTION_VIEW to go view the image.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491