Android 12 (SDK > 29) Extra_Output for ACTION_IMAGE_CAPTURE not working
Asked Answered
C

1

6

I want to take a picture with the camera and then send it to another activity. Everything works on the emulator without any problems. But as soon as I try the app on my real device, I always get the status code 0 in the ActivityResultLauncher and the If branch is not called. On the other hand, on the emulator I get -1 and the intent for calling the second activity started. What am I doing wrong ?

I use Android 12 (SKD 31) on real device

I have now also tested it with an emulator skd > 29 -> same problem. As soon as I press the Ok button in the Camera View for an image, Android throws me back to the MainActivity and not, as expected and desired, to the AddNewPlace activity. No error in debuger/logcat. As soon as I use a version SDK < 29 everything works without problems. I think it has something to do with the FileProvide or with the EXTRA_OUTPUT in the intent.

Manifest.xml

enter image description here

This one, also won't work

enter image description here

The code for the camera:

      File file = new File(Environment.getExternalStorageDirectory(),
                        UUID.randomUUID()+".jpg");

      outputFileUri = FileProvider.getUriForFile(MainActivity.this,BuildConfig.APPLICATION_ID + ".fileprovider",file);
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
      startActivityIntent.launch(intent);




       ActivityResultLauncher<Intent> startActivityIntent = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if(result.getResultCode() == RESULT_OK) // 0 on real device , -1 on Emulator
                {
                    if(result.getData() != null)

                    {
                        Log.e("FILE : ", outputFileUri.toString());
                        
                        Intent intent = new Intent(MainActivity.this,AddNewPlace.class);
                        intent.putExtra("File",outputFileUri.toString());

                        startActivity(intent);
                    }
                }

            }
        });

EDIT

Updated code, doesn't work either. Do I have to change something in the FileProvider?

File file  = 
MainActivity.this.getExternalFilesDir(UUID.randomUUID()+".jpg");
Commencement answered 23/5, 2022 at 10:10 Comment(3)
File file = new File(Environment.getExternalStorageDirectory(), UUID.randomUUID()+".jpg"); On Android 11+ devices you cannot create files in root of external storage. Use a public directory on external storage instead.Tasteful
Ok, I changed that. Nevertheless, the onActivityResult resultCode is 0. Do I have to change something here with Android 11+? outputFileUri = FileProvider.getUriForFile(MainActivity.this,BuildConfig.APPLICATION_ID + ".fileprovider",file);?Commencement
We cannot see what you changed and how. You should have posted your new code. And not in a comment!Tasteful
C
17

Add these lines in Manifests files

<queries>
        <intent>
            <action android:name="android.media.action.IMAGE_CAPTURE" />
        </intent>
    </queries>
Concent answered 23/5, 2022 at 18:27 Comment(1)
Modified original postCommencement

© 2022 - 2024 — McMap. All rights reserved.