【问题标题】:How to open excel, doc files with intent from my app in MS-Excel or MS-Word - Android如何在 MS-Excel 或 MS-Word 中从我的应用程序中打开 excel、doc 文件 - Android
【发布时间】:2023-03-29 15:05:01
【问题描述】:

我的要求是在我的应用程序中下载和查看 Excel、Doc 文件。所以我在手机存储中下载了 Excel/Doc 文件,并通过传递文件路径调用了 ACTION_VIEW Intent。然后我收到这个错误提示“尝试将文件保存在设备上然后打开它。”

如果有人能建议我另一种方法来打开 excel 或 doc 文件,我会很高兴。请各位,到目前为止我已经为此搜索了很多我没有找到解决方案,我确信我已经使用了正确的意图来打开文件。

我的代码在哪里:

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

                }

【问题讨论】:

  • 问题解决了吗?
  • 没有,先生?打开文件是直接操作。但是,没有按照应有的方式工作。
  • 任何解决方案

标签: android excel android-intent


【解决方案1】:

您需要标志 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);

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,我已经解决了。问题很可能与您的 ContentProvider/FileProvider 实现有关。我的 Excel 表格在 Google 表格中打开得很好,但在 MS Excel 中出现了这个错误。解决方法是从您的提供者返回正确的内容类型(通过 getType() 方法)。返回错误的内容类型会导致错误。

    试试这个代码看看它是否有帮助......如果有帮助,您可以完全实现所有类型文件的例程。

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

    祝你好运!

    【讨论】:

    • 我必须在哪里覆盖这个?
    • 在您的 ContentProvider 实现中
    【解决方案3】:

    尝试从文件中获取 mime 类型

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

    【讨论】:

      【解决方案4】:

      我没有用意图打开Doc,PPT,Excel文件最后我找到了更好的解决方案,请检查这里的代码

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

      结果如下:查看截图

      【讨论】:

        猜你喜欢
        • 2014-07-02
        • 2011-06-08
        • 1970-01-01
        • 1970-01-01
        • 2011-05-16
        • 2012-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多