Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android camera capture activity returns null Uri

This code worked on samsung before but now that i'm using Nexus One with Android 2.3.6, it's crashing as soon as I take a picture and click ok or choose a photo from gallery. Stacktrace shows a null pointer exception on the Uri.
My code for the activating the camera is as follows:

public void activateCamera(View view){      
    Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // start the image capture Intent
    startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if ((requestCode == CHOOSE_IMAGE_ACTIVITY_REQUEST_CODE  || requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) 
        && resultCode == RESULT_OK && null != data) {

        selectedImage = data.getData();

        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);

        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);

        String picturePath = cursor.getString(columnIndex);

        cursor.close();

        Bitmap bits = null;

        BitmapFactory.Options options = new BitmapFactory.Options();

        options.inSampleSize = inSampleSize;

        try {
            bits = BitmapFactory.decodeStream(new FileInputStream(picturePath),null,options);
        } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }

Any idea what could be the problem? Thanks!

like image 697
jop Avatar asked Aug 29 '13 02:08

jop


2 Answers

You have to tell the camera, where to save the picture and remeber the uri yourself:

private Uri mMakePhotoUri;

private File createImageFile() {
    // return a File object for your image.
}

private void makePhoto() {
    try {
        File f = createImageFile();
        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        mMakePhotoUri = Uri.fromFile(f);
        i.putExtra(MediaStore.EXTRA_OUTPUT, mMakePhotoUri);
        startActivityForResult(i, REQUEST_MAKE_PHOTO);
    } catch (IOException e) {
        Log.e(TAG, "IO error", e);
        Toast.makeText(getActivity(), R.string.error_writing_image, Toast.LENGTH_LONG).show();
    }
}

@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    switch (requestCode) {
        case REQUEST_MAKE_PHOTO:
            if (resultCode == Activity.RESULT_OK) {
                // do something with mMakePhotoUri
            }
            return;
        default: // do nothing
            super.onActivityResult(requestCode, resultCode, data);
    }
}

You should save the value of mMakePhotoUri over instance states withing onCreate() and onSaveInstanceState().

like image 164
flx Avatar answered Sep 27 '22 17:09

flx


Inject this extra into the Intent that called onActivityResult and the system will do all the heavy lifting for you.

File f = createImageFile();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));

Doing this makes it as easy as this to retrieve the photo as a bitmap.

private void handleSmallCameraPhoto(Intent intent) {
    Bundle extras = intent.getExtras();
    mImageBitmap = (Bitmap) extras.get("data");
    mImageView.setImageBitmap(mImageBitmap);
}
like image 27
TheDudeAbides Avatar answered Sep 27 '22 16:09

TheDudeAbides