Error while using android camera inside application

Asked

Viewed 40 times

0

I’m implementing an app where I need to use the camera to take a photo and save it in the gallery, however I’m getting an error when I try to run a command to do this procedure.

public class CaptureImage extends AppCompatActivity{

    private static final int CAMERA_IMAGE = 2;
    private ImageView imageView;
    private Uri uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_capture_image);

        imageView = (ImageView) findViewById(R.id.ivImgSelected);
    }

    public void useCamera(View view) {

        Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        uri  = Uri.fromFile(Util.createImage()); // Util.createImage() => Cria um arquivo com o endereço padrão da galeria
        it.putExtra(MediaStore.EXTRA_OUTPUT, uri); // erro acontece aqui
        startActivityForResult(it, CAMERA_IMAGE);
    }
}

After some tests, it is concluded that the error happens when I try to execute the command it.putExtra(MediaStore.EXTRA_OUTPUT, uri) within the method useCamera(View view). I removed this line of code and the app opens the camera normally. The problem is that I need this command for my application to save the file properly, without it the application does not save the image. Can someone help me with this problem?

The error presented is:

android.os.Fileuriexposedexception: file://Storage/Emulated/0/Pictures/1715784088086.jpg

The SDK version I’m using to run the app is API 26

  • 1

    But what’s the mistake?

  • Could you please edit the question with the error you are showing, and also inform which targetSdkVersion of the app. Could be some access permission issue.

  • Sorry for the delay. I just edited the question detailing the error and the version of the SDK used.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.