androidandroid-intentxamarin.androidxamarinkingsoft

How to open .xlsx files using Intents in Android?


I am using the following Intent to open .xlsx file using an Intent Chooser. I have Kingsoft Offie and Polaris office at my disposal to open these files.

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

Where OSUtils.GetCalcFilePath is defined as

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

and OSUtils.GetMimeType is defined as

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 "*/*";
}

Now when I run it, I get a dialog(chooser) giving me two options, "Kingsoft Office" and "Polaris Office". Choosing Polaris Office gives me a toast saying "Not a supported document type" and on the other hand choosing Kingsoft Office just opens up the Kingsoft App and does nothing. The document is not opened in Kingsoft Office. Whereas, if I go to my file manager and tap on the .xlsx file, it opens up perfectly in both the office apps. I checked my code and all the mimetypes and paths are correct and are pointing to the desired file. Any ideas?

Thanks in Advance

EDIT

It seems using SetData and SetType individually wont work here. They are mutually exclusive i.e calling one clears the other. "SetDataAndType" is the way to go here. :)


Solution

  • It seems using SetData and SetType individually wont work here. They are mutually exclusive i.e calling one clears the other. "SetDataAndType" is the way to go here. :)