Search code examples
androidandroid-dialogfile-formatandroid-download-manager

Want to open downloaded file in appropriate application in Android


I have to download a document from my server, to my app and save into a private folder.

The document can be anything like .doc, .pdf, .docs, .jpg etc.

Now, what I want is, after the download is completed, I show an Alert Dialog to open the document.

When I click on the pen button then open that particular file in an appropriate application and if the app is not available, then show Dialog to download app.

Give me some hint on this.

Thanks in advance.


Solution

  • Try this way:

    Mails attachment open

            File file = new File(yourdownloadfilePATH);
    
            if ((download_file_name.endsWith(".pdf")) || (download_file_name.endsWith(".PDF"))){            
    
                if(file.exists() && file.length()!=0){
    
                Intent intent = new Intent();
    
                intent.setClassName("com.adobe.reader","com.adobe.reader.AdobeReader");
    
                intent.setAction(android.content.Intent.ACTION_VIEW);
    
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
                Uri uri = Uri.fromFile(file);
    
                intent.setDataAndType(uri, "application/pdf");
    
                try {
    
                    startActivity(intent);
    
                } catch (Exception e) {
    
                                    AlertDialog.Builder builder = new AlertDialog.Builder(youractivity.this);
                                    builder.setTitle("No Application Found");
                                        builder.setMessage("Download application from Android Market?");
                                        builder.setPositiveButton(
                                                "Yes, Please",
                                                new DialogInterface.OnClickListener() {
                                                    @Override
                                                    public void onClick(
                                                            DialogInterface dialog,
                                                            int which) {
                                                        Intent marketIntent = new Intent(
                                                                Intent.ACTION_VIEW);
                                                        marketIntent.setData(Uri
                                                                .parse("market://details?id=com.adobe.reader"));
                                                        startActivity(marketIntent);
                                                    }
                                                });
                                        builder.setNegativeButton("No, Thanks",
                                                null);
                                        builder.create().show();
                }
    
        }
     }  else if ((download_file_name.endsWith(".jpg"))||  (download_file_name.endsWith(".bmp"))||
    (download_file_name.endsWith(".BMP"))||(download_file_name.endsWith(".png"))||  (download_file_name.endsWith(".PNG"))||(download_file_name.endsWith(".gif"))||   (download_file_name.endsWith(".GIF"))||
                                (download_file_name.endsWith(".JPG"))) {
    
            Intent intent = new Intent();
            intent.setAction(android.content.Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(file), "image/*");       
            startActivity(intent);
    
        }
    else if ((download_file_name.endsWith(".txt"))) {
    
    
        Intent intent = new Intent();
        intent.setAction(android.content.Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file), "text/html");
        startActivity(intent);
    
    }