FileUriExposedException error on Android N

5

Hello friends I'm 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, hit a photo and record the photo in an ImageView, worked until Android 6.0, but now in Android 7 has stopped working, does anyone know how to solve?

Below is my code:

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);
        }
    }
}

Thank you!

    
asked by anonymous 01.03.2017 / 06:04

1 answer

12

Android N has undergone some behavior changes , including permissions changes impacting file sharing .

Applications for Android N are now unable to expose URIs of type file:// out of the application, using a URI of type content:// and granting it a temporary access permission. The easiest way to do this is to use a FileProvider .

Its implementation is done in 3 steps:

  • 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>
    
  • Specify shared directories - Create a xml , in the res/xml/ folder, with the name indicated in step 1 in android:resource="@xml/filepaths" . It should include, by each directory, an XML element of 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>
    
  • #

    Example:

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

    01.03.2017 / 11:28