How to open excel, doc files with intent from my app in MS-Excel or MS-Word - Android

Viewed 2311

My requirement is to download and view the Excel, Doc files in my app. So I downloaded the Excel/Doc file in phone storage and called the ACTION_VIEW Intent by passing the file path. Then I got this error saying "Try saving the file on the device and then opening it."

This is Error ScreenShot

I can be glad if any one can suggest me another alternatives as well to open excel or doc files. Please guys i have searched a lot for this so far i didn't find the solution and i am sure that i have used proper intent for opening files.

Where My Code:

Intent intentpdf = new Intent(Intent.ACTION_VIEW);
                intentpdf.setDataAndType(Uri.parse(message), "application/msword");
                intentpdf.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                try {
                    mactivity.startActivity(intentpdf);
                } catch (ActivityNotFoundException e) {


                    if (!mactivity.isFinishing())
                        Toast.makeText(mactivity, "Install MSWord Viewer.", Toast.LENGTH_LONG).show();

                }


Intent intentpdf = new Intent(Intent.ACTION_VIEW);
                intentpdf.setDataAndType(Uri.parse(message), "application/vnd.ms-excel");
                intentpdf.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                try {
                    mactivity.startActivity(intentpdf);
                } catch (ActivityNotFoundException e) {


                    if (!mactivity.isFinishing())
                        Toast.makeText(mactivity, "Install Excel Viewer.", Toast.LENGTH_LONG).show();

                }
4 Answers

You need the flags FLAG_ACTIVITY_NO_HISTORY,FLAG_GRANT_READ_URI_PERMISSION, FLAG_GRANT_WRITE_URI_PERMISSION.

intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION );

nothing is worked for me to open the Doc,PPT,Excel files with intent at last I find a better solution , please check the code here

if(fileType.equalsIgnoreCase("doc") || fileType.equalsIgnoreCase("docx")) {
                            String str = "com.microsoft.office.word";

                            Uri pathe = Uri.fromFile(fileN);
                            Intent intent = new Intent();
                            intent.setAction(Intent.ACTION_VIEW);
                            intent.setDataAndType(pathe, "application/msword");
                            PackageManager packageManager = activity.getPackageManager();
                            if (intent.resolveActivity(packageManager) != null) {
                                activity.startActivity(intent.createChooser(intent, "Choose app to open document"));
                            }
                            else
                            {
                                //Launch PlayStore
                                try {
                                    activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+str)));

                                } catch (android.content.ActivityNotFoundException n) {
                                    activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id="+str)));
                                }
                            }

Here is the result : check the screenshot enter image description here

Related