【问题标题】:How to open .xlsx files using Intents in Android?如何在 Android 中使用 Intents 打开 .xlsx 文件?
【发布时间】:2013-07-04 10:10:10
【问题描述】:

我正在使用以下 Intent 使用 Intent 选择器打开 .xlsx 文件。我可以使用 Kingsoft Offie 和 Polaris office 来打开这些文件。

var calcIntent = new Intent ();             
calcIntent.SetAction (Intent.ActionView);
Android.Net.Uri fileUri = Android.Net.Uri.FromFile (new File (OSUtils.GetCalcFilePath (id)));
calcIntent.SetData (fileUri);
var mimeType = OSUtils.GetMimeType (fileUri.ToString ());
calcIntent.SetType (mimeType);                  
try {
     StartActivity (Intent.CreateChooser (calcIntent, "Open Via"));
 } catch (ActivityNotFoundException) {
    Toast.MakeText (this, "You do not have Kingsoft Office Installed!", ToastLength.Long).Show();
}

其中 OSUtils.GetCalcFilePath 定义为

public static string GetCalcFilePath (int currentId) {
    var calcDirPath = OSUtils.GetCalcDirForEstimate (currentId);
    var calcSheetName = String.Format ("builder_calc_{0}.xlsx", currentId);
    var calcSheet = new Java.IO.File (calcDirPath, calcSheetName);
    if (!calcSheet.Exists ()) {
        calcSheet.CreateNewFile ();
    }
    return calcSheet.Path;
}

并且 OSUtils.GetMimeType 被定义为

public static string GetMimeType (string fileUri) {
    String mimeType = null;
    var extension = MimeTypeMap.GetFileExtensionFromUrl (fileUri);
    if (extension != null) {
        mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension (extension);
    }
    if (mimeType != null)
  return mimeType;
    else
        return "*/*";
}

现在当我运行它时,我得到一个对话框(选择器),给我两个选项,“Kingsoft Office”和“Polaris Office”。选择 Polaris Office 让我敬酒说“不支持的文档类型”,另一方面,选择金山 Office 只是打开金山软件,什么都不做。文档在金山办公中没有打开。 然而,如果我去我的文件管理器并点击 .xlsx 文件,它会在两个办公应用程序中完美打开。我检查了我的代码,所有的 mimetypes 和路径都是正确的,并且指向了所需的文件。有什么想法吗?

提前致谢

编辑

似乎单独使用 SetData 和 SetType 在这里不起作用。它们是互斥的,即调用一个清除另一个。 “SetDataAndType”是这里的方法。 :)

【问题讨论】:

  • 文件路径问题,您没有正确传递
  • 我正确传递了它。我发现了我的问题。

标签: android android-intent xamarin.android xamarin kingsoft


【解决方案1】:

似乎单独使用 SetData 和 SetType 在这里不起作用。它们是互斥的,即调用一个清除另一个。 “SetDataAndType”是这里的方法。 :)

【讨论】:

    猜你喜欢
    • 2017-09-11
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 2017-06-10
    • 2016-09-22
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    相关资源
    最近更新 更多