Vinicius Mangueira
Vinicius Mangueira

Reputation: 319

Android Cam Fatal Excpetion fileURIExposed

String caminhoFoto = getExternalFilesDir(null) + "/"+ System.currentTimeMillis() +".jpg";
            File arquivoFoto = new File(caminhoFoto);
            intentImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(arquivoFoto));

I write this code, and the error is below, I already added the provider, no manifest and nothing, continues the error that is without any code, I was told that using the content

Erro:

FATAL EXCEPTION: main
              Process: br.com.viniciusdeep.agenda, PID: 22038
              android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/br.com.viniciusdeep.agenda/files/1524003224785.jpg exposed beyond app through ClipData.Item.getUri()

Upvotes: 1

Views: 59

Answers (1)

Amani
Amani

Reputation: 3977

in Android 7.0, the file scheme on a Uri is banned, in effect. If you attempt to pass a file: Uri in an Intent that is going to another app, you will crash with a FileUriExposedException exception.

you will face similar issues with putting file: Uri values on the clipboard in ClipData

This is coming from an updated edition of StrictMode. StrictMode.VmPolicy.Builder has a penaltyDeathOnFileUriExposure() method that triggers the detection of file: Uri values and the resulting FileUriExposedException exceptions. And, it appears that this is pre-configured, much as how StrictMode is pre-configured to apply penaltyDeathOnNetwork() (the source of your NetworkOnMainThreadException crashes).

However, this only kicks in if your targetSdkVersion is set to 24 or higher. At that point, you will need to find other ways of getting your content to other apps, such as via a class called FileProvider Or, you can also disable the check by configuring your own StrictMode.VmPolicy and skipping directFileUriExposure(), though this is not a great solution.

Upvotes: 1

Related Questions