Skip to content Skip to sidebar Skip to footer

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 th

Solution 1:

Try this way:

Mails attachment open

Filefile=newFile(yourdownloadfilePATH);

        if ((download_file_name.endsWith(".pdf")) || (download_file_name.endsWith(".PDF"))){            

            if(file.exists() && file.length()!=0){

            Intentintent=newIntent();

            intent.setClassName("com.adobe.reader","com.adobe.reader.AdobeReader");

            intent.setAction(android.content.Intent.ACTION_VIEW);

            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            Uriuri= Uri.fromFile(file);

            intent.setDataAndType(uri, "application/pdf");

            try {

                startActivity(intent);

            } catch (Exception e) {

                                AlertDialog.Builderbuilder=newAlertDialog.Builder(youractivity.this);
                                builder.setTitle("No Application Found");
                                    builder.setMessage("Download application from Android Market?");
                                    builder.setPositiveButton(
                                            "Yes, Please",
                                            newDialogInterface.OnClickListener() {
                                                @OverridepublicvoidonClick(
                                                        DialogInterface dialog,
                                                        int which) {
                                                    IntentmarketIntent=newIntent(
                                                            Intent.ACTION_VIEW);
                                                    marketIntent.setData(Uri
                                                            .parse("market://details?id=com.adobe.reader"));
                                                    startActivity(marketIntent);
                                                }
                                            });
                                    builder.setNegativeButton("No, Thanks",
                                            null);
                                    builder.create().show();
            }

    }
 }  elseif ((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"))) {

        Intentintent=newIntent();
        intent.setAction(android.content.Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file), "image/*");       
        startActivity(intent);

    }
elseif ((download_file_name.endsWith(".txt"))) {


    Intentintent=newIntent();
    intent.setAction(android.content.Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file), "text/html");
    startActivity(intent);

}

Solution 2:

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

Intentintent=newIntent();
intent.setAction(android.content.Intent.ACTION_VIEW);
Filefile=newFile(your_file);
Stringextension= file .split("\\.");

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

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

//For video extensions like mp4 or any video fileselseif(extension == mp4){
 intent.setDataAndType(Uri.fromFile(file), "video/*");
}
startActivity(intent); 

Post a Comment for "Want To Open Downloaded File In Appropriate Application In Android"