2

I'm trying to display the photo just taken by camera and display it in a imageView. This is the method that has the problem:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 100 && resultCode == RESULT_OK) {

        selectedImage = fileUri;//data.getData();

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

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

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

        // The following three lines work perfectly :-) 
        // if I comment the part of the cursor lines above
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        ImageView imageView = (ImageView) findViewById(R.id.Imageprev);
        imageView.setImageBitmap(photo);
    }
}

When debugging I see some values like for example

selectedImage : file:///mnt/sdcard/external_sd/Pictures/MyCameraApp/IMG_20150530_032752.jpg

and I think this is ok. Another interesting value is

filePathColumn : _data

is this value an expected one? You tell me, please.

So, cursor is null and the line

cursor.moveToFirst();

spits the error null pointer :-/. I'm debugging code in real device with Android 2.2. Help.

EDITION

this is the method that call the previous one

private void clickpic() {

    // Check Camera
        if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {

            // Open default camera
            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            /*
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

            // start the image capture Intent
            startActivityForResult(intent, 100);
            */

            fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create

            //the getOutputMediaFileUri is implemented as saving media file suggests by developer.android.com
            //intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, fileUri); // set the image file

            // start the image capture Intent
            startActivityForResult(intent,100);

        } else {
            Toast.makeText(MainActivity.this, "Camera not supported", Toast.LENGTH_LONG).show();
        }
    }

The commented code are my failed attempts.

JoeCoolman
  • 512
  • 3
  • 8
  • 27

1 Answers1

0

You need to put the fileUri as an extra as shown here Android Developers

fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

and try replacing your onActivityResult with:

if (requestCode == 100 && resultCode == RESULT_OK) {

    selectedImage = fileUri;

    Bitmap photo = BitmapFactory.decodeFile(new File(selectedImage));
    ImageView imageView = (ImageView) findViewById(R.id.Imageprev);
    imageView.setImageBitmap(photo);
}
Kamran Ahmed
  • 7,661
  • 4
  • 30
  • 55
  • yes, I tried that as you see in my commented code above. Doing your suggestion makes data=null in onActivityResult() part :-/ . I'll return later, it's late here. – JoeCoolman Jun 01 '15 at 05:43
  • 1
    onActivityResult, rather than using `ContentResolver` to get the path from uri, you may directly use `new File(fileUri).getAbsolutePath()` – Kamran Ahmed Jun 01 '15 at 06:35
  • After taking the picture, my camera app shows me two buttons, Cancel and OK. It seems when I press OK, the camera app is killed without sending the intent image to onActivityResult() so, data is null. :-/ damn. I will try this approach http://stackoverflow.com/questions/12952859/capturing-images-with-mediastore-action-image-capture-intent-in-android – JoeCoolman Jun 04 '15 at 05:39
  • That was the exact page that I pointed you to in my answer. That guide is very easy to follow and show multiple ways of acheiving what you want to. – Kamran Ahmed Jun 04 '15 at 07:17
  • I think my camera app hasn't access to mnt/sdcard/external_sd/Pictures that's why data is null in onActivityResult method. When take pictures, my camera save photos in /mnt/sdcard/DCIM/Camera/. The first path is suggested by android developer page, what can I do? – JoeCoolman Jul 04 '15 at 02:22
  • I've solved it. I've created a global Intent field, and work with it inside of onActivityResult(), do not use Intent data ;-) – JoeCoolman Jul 04 '15 at 03:20