【问题标题】:Is it possible to know if a document is opened/viewed by user in xamarin android app?是否可以知道用户是否在 xamarin android 应用程序中打开/查看了文档?
【发布时间】:2018-10-30 10:32:54
【问题描述】:

我需要检查用户是否查看了文档(pdf、xls、docx、ppt..等)。在某些用户的设备中,没有应用程序可以打开某些文档类型。在这种情况下,我需要知道用户没有打开/查看文档。

这是一个使用 xamarin 表单开发的安卓应用。使用下面的代码打开文档。如果有一个应用程序可以打开相应的文档,这将正常工作。但是,如果没有应用程序可以打开文档,它会显示标题为“选择应用程序:”的空白活动

    ...other code here...

  var chooserIntent = Intent.CreateChooser(intent, "Choose an Application:");
                activity.StartActivityForResult(chooserIntent, 10);

【问题讨论】:

    标签: android xamarin xamarin.forms document


    【解决方案1】:

    您可以通过Android文件MIME type查看。参考以下代码

    Dictionary<string, string> matchArray = new Dictionary<string, string>();
    
    matchArray.Add(".3gp", "video/3gpp");
    matchArray.Add(".apk", "application/vnd.android.package-archive");
    matchArray.Add(".doc", "application/msword");
    matchArray.Add(".gif", "image/gif");
    matchArray.Add(".wps", "application/vnd.ms-works");
    matchArray.Add(".pdf", "application/pdf");
    matchArray.Add(".ppt", "application/vnd.ms-powerpoint");
    matchArray.Add(".png", "image/png");
    //... 
    
    public void OpenFileByPath(Context context, string path)
    {
      if (context == null || path == null)
        return;
    
      string type = "";
      foreach (string key in matchArray.Keys)
       {
    
         if (path.Contains(key))
           {
              type = matchArray[key];
              break;
           }
        }
    
      if (type == "")
        {
           //there is no app can open the file ,do some you want 
        }
      else
        {
           // open your file 
        }
    }
    

    您可以通过互联网搜索添加更多关于 MIME 类型的键值

    【讨论】:

    • 我认为这个 sol 不适用于我的场景。例如:在我的应用程序中,如果有一个“.doc”文件并且用户试图打开它并且用户的手机上没有安装应用程序来打开文件,那么该文件没有打开但是上面的代码给了我有效的类型值作为文档路径将具有密钥。如果我在这里误解了什么,请告诉我!我需要知道文件是否可以在用户的​​手机上打开。
    • if (type == "") 表示用户设备上没有可以打开文件的应用
    • 对不起,我没有明白你的意思。我们只是将类型添加到 matchArrasy 所以例如我们添加了 .doc 之类的 matchArray.Add(".doc", "application/msword");所以行 matchArray[key];总是返回返回一个值。我们在哪里检查是否有某个应用可以在用户设备上打开我们的文档?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    • 2012-04-20
    • 2017-02-11
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    相关资源
    最近更新 更多