How to open excel, doc files with the intention of my application in MS-Excel or MS-Word - Android

My requirement is to download and view Excel, Doc files in my application. So I downloaded the Excel / Doc file to the phone’s memory and named ACTION_VIEW Intent, passing the file path. Then I got this error: "Try to save the file on the device, and then open it."

This is Error ScreenShot

I can be glad if anyone can offer me another alternative to open excel or doc files. Please guys, I have searched a lot for this so far, I have not found a solution, and I'm sure I used the right intention to open the files.

Where is 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
source share
2 answers

, . , , ContentProvider/FileProvider. Excel Google , MS Excel. , ( getType()) . .

, , ... , .

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

!

0

mime

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

Source: https://habr.com/ru/post/1682822/


All Articles