Android – I can't open a file from an attempt but from the file explorer


Good day,

I am making an app in which I have to load from the database some cardview with data and image or document, when pressing on this element to obtain the option to download the file and when finished downloading it, offer the possibility of opening it from the application. All that works well until the moment of opening the files (Images, word documents, excel, etc.), at this point it does not show me any error either by the android studio console or by the app as such, only when opening the it comes out as corrupt (when it is an image, although if I open it from the file explorer it opens without problems) and when it is a document it opens as if the file was empty or as if it were a new one.

So I am taking the data through the download manager:

private void executeDownload() {
    ProgressDialog progressDialog = new ProgressDialog(AssignedTasksDetailActivity.this);
    progressDialog.setMessage("Descargando Archivo...");
    progressDialog.setCancelable(false);
    progressDialog.show();
    // registrer receiver in order to verify when download is complete
    registerReceiver(new DonwloadCompleteReceiver(), new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url_app));
    request.setDescription("Descargando Archivo " + filename);
    request.setTitle("Descargado "+ filename);
    // in order for this if to run, you must use the android 3.2 to compile your app
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    }
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
    // get download service and enqueue file
    DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    manager.enqueue(request);
    progressDialog.dismiss();
}

Then I show an alertdialog when the download is complete to give the option to open the file (this is where I think it fails):

public class DonwloadCompleteReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)){
            final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(AssignedTasksDetailActivity.this);
            alertDialogBuilder.setTitle("Importante");
            alertDialogBuilder.setMessage("¿Desea abrir este archivo?");
            alertDialogBuilder.setCancelable(true);
            alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                }
            });

            alertDialogBuilder.setPositiveButton("Abrir",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    cancel();
                    Coop app = ((Coop)getApplicationContext());
                    //File file = new File(filename);  // -> filename = maven.pdf
                    File file = new File(Environment.DIRECTORY_DOWNLOADS, filename);
                    Uri path = FileProvider.getUriForFile(AssignedTasksDetailActivity.this, "com.example.coop.fileprovider", file);
                    String type = url_app.substring(url_app.lastIndexOf('.'), url_app.length());
                    Intent intent = new Intent(Intent.ACTION_VIEW);


                    if(type==".jpg" || type==".jpeg" || type==".png"){
                        intent.setDataAndType(path, "image/*");
                    }else{
                        if(type==".pdf"){
                            intent.setDataAndType(path, "application/pdf");
                        }else{
                            intent.setData(path);
                        }
                    }
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    try{
                        AssignedTasksDetailActivity.this.startActivity(intent);
                        cancel();
                    }catch(ActivityNotFoundException e){
                        Toast.makeText(AssignedTasksDetailActivity.this, "No hay una aplicacion instalada para abrir este tipo de archivos", Toast.LENGTH_SHORT).show();
                        cancel();
                    }
                }
            });
            alertDialogBuilder.show();
        }
    }
}

I attached my fileprovider configuration to see if that has anything to do with it:


    

And its xml:



    
    
    
    
    
    


Try to see the file path with a toast and this is what throws me: "content: //com.example.coop.fileprovider/root/Download/el_file_name.extension".

Thanks in advance.