Fileuriexposedexception error on Android N

Asked

Viewed 5,927 times

9

Hello friends I am facing the following error:

android.os.Fileuriexposedexception: file:///Storage/Emulated/0/Pictures/1488344088086.jpg Exposed Beyond app through Clipdata.item.getUri().

The intention is to open the camera, take a photo and record the photo in an Imageview, worked until Android 6.0, but now Android 7 stopped working, someone knows how to solve ?

Follow my code below:

private DialogInterface.OnClickListener onOriginSelect() {
    return new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case 0:
                    if (PermissionUtils.checkPermission(PeopleActivity.this, CAMERA_REQUEST_CODE, permissoes)) {
                        File diretorio = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                        String nomeImagem = diretorio.getPath() + "/" + System.currentTimeMillis() + ".jpg";
                        file = new File(nomeImagem);
                        intentGlobal = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intentGlobal.putExtra(MediaStore.EXTRA_OUTPUT, uri.fromFile(file));
                        startActivityForResult(intentGlobal, CAMERA_REQUEST_CODE);
                    } else {
                        Toast.makeText(PeopleActivity.this, "ERRO CAM PERM", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case 1:
                    if (PermissionUtils.checkPermission(PeopleActivity.this, GALERIA_REQUEST_CODE, permissoes)) {
                        intentGlobal = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(intentGlobal, CAMERA_REQUEST_CODE);
                    } else {
                        Toast.makeText(PeopleActivity.this, "PERM GALERIA", Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        }
    };
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case GALERIA_REQUEST_CODE:
                Uri uri = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
                if (cursor.moveToFirst()) {
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String path = cursor.getString(columnIndex);
                    cursor.close();
                    file = new File(path);
                    break;
                }
                Glide.with(getBaseContext()).load(file.getPath()).asBitmap().centerCrop().into(imagePeople);
        }
    }
}

Grateful !!!

1 answer

25


Android N suffered some behavioural changes, among them changes to permissions impacting on file sharing.

Android N apps are now unable to expose Uris of the type file:// outside the application, using a URI such as content:// and grant him a temporary access permit.
The easiest way to do this is to use a Fileprovider.

Its implementation is done in 3 steps:

  1. Specify Fileprovider on Androidmanifest.

    <application
        ...>
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.example.myapp.fileprovider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths" />
        </provider>
        ...
    </application>
    
  2. Specify shared directories - Create a file xml, in the briefcase res/xml/, with the name indicated in step 1 in android:resource="@xml/filepaths". An XML element of the type <paths> </paths>

    Example:

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <external-path name="external_files" path="."/>
    </paths>
    
  3. Get the URI for the file - Instead of Uri.fromFile() should use FileProvider.getUriForFile() that returns a type URI content://, as required by the new model.

    Example:

    fileUri = FileProvider.getUriForFile(
            MainActivity.this,
            "com.example.myapp.fileprovider",
            requestFile);
    

References:

Browser other questions tagged

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