Question

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.

Was it helpful?

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

}

OTHER TIPS

Not sure, but I think this will help you a little:

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(your_file);
String extension = file .split("\\.");

//For text extensions like text or any textfiles
if(extension == txt){
 intent.setDataAndType(Uri.fromFile(file), "test/*");
}

//For audio extensions like  mp3 or any audio files
else if(extension == mp3 ){ 
 intent.setDataAndType(Uri.fromFile(file), "audio/*");
}

//For video extensions like mp4 or any video files
else if(extension == mp4){
 intent.setDataAndType(Uri.fromFile(file), "video/*");
}
startActivity(intent); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top