'How to delete a downloaded APK file in the download folder Android 10

How to delete file programmatically in android java ? I have used the app updater package to download my .apk file but the package did not delete the downloaded .apk file after it has installed it. This is my code for the app updater

private class checkForUpdateAsyncTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... args) {

            new AppUpdater(mContext)
                    .setUpdateFrom(UpdateFrom.JSON)
                    .setUpdateJSON("http://172.16.206.19/REST_API/json_format.json")
                    .setDisplay(Display.DIALOG)
                    .setCancelable(false) // Dialog could not be dismissable
                    .showAppUpdated(true)
                    .setButtonDoNotShowAgain(null)
                    .start();


        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        pb.setVisibility(View.INVISIBLE);
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pb.setVisibility(View.VISIBLE);
        try {
            deleteDownloadedFiles();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

What ive tried so far :

File dl_path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    File[] files = dl_path.listFiles();

    for (File i :files ) {
        Boolean result = i.delete();
        if(result){
            Toast.makeText(MainActivity.this, i.getName()+" has been deleted", Toast.LENGTH_SHORT).show();
        }
    }


    return;

this will return a false result and the file is not deleted.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source