3

I'm trying to retrieve a file from my ACTION_IMAGE_CAPTURE activity, but when I use

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

I get a null intent on my activity result. Here is the full code.

The intent

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(cameraIntent, 1888);

My onActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{  
    if (requestCode == 1888 && resultCode == RESULT_OK)
    {
        Uri uri = (Uri)data.getData();
        Intent i = new Intent(getApplicationContext(), Camara.class);
        i.putExtra("uri", uri);
        startActivity(i);
    }
}

i'm using the following permissions

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

i need to access to the file of the image captured but i'm getting null on my parameter data

and then, How do I get the file? Like this?

File myfile = new File(uri.getPath());

I also tried this:

Uri uri = (Uri)data.getData();
Intent i = new Intent(getApplicationContext(), Camara.class);
i.putExtra("uri", uri);
startActivity(i);

In this case I can get the bitmap with this

Bitmap lafoto = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);

but i can't get the File

I tried to follow this solution but didn't work for me Get Path of image from ACTION_IMAGE_CAPTURE Intent

Community
  • 1
  • 1
Clamari
  • 353
  • 4
  • 17
  • 1
    `get a null intent on my activity result`. If that is true than there will be a NullPointerException. Is there? You are sure that data==null? Or do you mean that data.getData()==null? – greenapps Feb 19 '15 at 21:22
  • when i debug i see data=null explicit – Clamari Feb 20 '15 at 15:16
  • So you have a NullPointerExacption!? – greenapps Feb 20 '15 at 15:22
  • Yes, i guess that when you use the cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); the camera app will always return null data and write the jpg file into the Uri given – Clamari Feb 21 '15 at 17:09
  • Did you get this last one to work? I was unsuccessfull with it. First, the picture cannot be approved by checking the ok check. Second, which uri would be given here? MediaStore.Images.Media.EXTERNAL_CONTENT_URI.getPath() delivers /external/images/media. – greenapps Feb 22 '15 at 08:49
  • Quick unrelated note: You do not need to list the READ_EXTERNAL_STORAGE permission in your manifest. According to the android docs: `The write permission implicitly allows reading, so if you need to write to the external storage then you need to request only one permission:` https://developer.android.com/training/camera/photobasics.html – w3bshark Oct 11 '16 at 14:28

2 Answers2

4

The problem was that when you give a extra_output, the file is written in the URI provided so you just access via that URI to use the image captured. Because that, no data is provided on the onActivityResult, because the camera app will only return a mini-bitmap as a Intent extra in the onActivityResult when no extra_output is provided in the camera app intent.

Clamari
  • 353
  • 4
  • 17
1

I had the same problem and changed my code flow like below;

capturePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, TAKE_PHOTO);
            }
        });

then

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case TAKE_PHOTO:
                if (resultCode == Activity.RESULT_OK) {
                    final Bitmap bitmap = (Bitmap) data.getExtras().get("data");
                    imageView.setImageBitmap(bitmap);
                }
                break;
        }
    }
oguzhan
  • 2,073
  • 1
  • 26
  • 23