简体   繁体   中英

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

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."

这是错误Scre​​enShot

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

                }

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

I had this exact same problem and I fixed it. The problem is most likely with your ContentProvider/FileProvider implementation. My Excel sheet opened fine in Google Sheets but I got this error in MS Excel. The fix was to return the proper content type (via the getType() method) out of your provider. Returning the wrong content type will cause the error.

Try this code to see if it helps...and if it does, you can fully implement the routine for files of all types.

    @Override
    public String getType(Uri uri) {
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    }

Good Luck!

Try get mime type from file

public String getMimeTypeByFile(String filePath) {
        MimeTypeMap type = MimeTypeMap.getSingleton();
        return type.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(filePath));
    }

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 在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM