I'm trying to implement auto download and install apk in my application using file provider. For android version less than Oreo it works fine but getting problem while running app on device with android 8.0 and above. It works fine till Nougat, but doesn't works with Oreo. Downloading apk from server working fine but fails to install new apk.
Here is my code.

```
File file, folder;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Utility.checkPermissionCamera(Main2Activity.this);
    Utility.checkPermissionInstall(Main2Activity.this);
        String fileName = "AutoDownloadApplication.apk";
        folder = new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString());
        Logger.e("FOLDER", " " + folder);
        file = new File(folder.getAbsolutePath(), fileName);
        Log.e("File ", "" + file);

        final Uri uri = FileProvider.getUriForFile(Main2Activity.this, BuildConfig.APPLICATION_ID + ".provider", file);
        Logger.e("Check URI ", "" + uri);
    if (Utility.checkPermissionCamera(Main2Activity.this)) {
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(StaticConstant.DOWNLOADAPK));
        request.setDescription("Downloading New Apk");
        request.setTitle(Main2Activity.this.getString(R.string.app_name));
        //set destination
        request.setDestinationInExternalFilesDir(Main2Activity.this, BuildConfig.APPLICATION_ID + ".provider", fileName);

        // get download service and enqueue file

            final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
            final long downloadId = manager.enqueue(request);
            //set BroadcastReceiver to install app when .apk is downloaded
            BroadcastReceiver onComplete = new BroadcastReceiver() {
                public void onReceive(Context ctxt, Intent intent) {

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
                    intent.setData(uri);
                    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    startActivity(intent);
                } else {
                    Intent install = new Intent(Intent.ACTION_VIEW);
                    install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    install.setDataAndType(uri,
                            manager.getMimeTypeForDownloadedFile(downloadId));
                    startActivity(install);
                }
                    unregisterReceiver(this);
                }
            };
            //register receiver for when .apk download is compete
            registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
        }
}

```

Manifest file:

```
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="MyPackageName">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<permission
    android:name="android.permission.REQUEST_INSTALL_PACKAGES"
    android:protectionLevel="normal" />

<uses-permission
    android:name="android.permission.REQUEST_DELETE_PACKAGES"
    tools:ignore="ProtectedPermissions" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="MyPackageName.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>

</application>

```

Here is the file\_paths xml:

```
<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="Download" path="Android/data/MyPackageName/files/Download"/>
</paths>

```