0

我试图让外部应用程序打开在运行时确定的不同文件类型。从我所看到的一切来看,我觉得我做得对,但没有一个程序能正确打开文件。对于使用文本文件的 Astro,它会显示此错误:java.lang.illegalargumentexception invalid uri used for observer。基本上它似乎没有得到正确的文件位置。任何帮助将不胜感激。

这是我的代码:

System.Net.WebClient webClient = new System.Net.WebClient();

  String attachmentPath = appManager.getAttachmentPath(plexState.getActiveBrain().getGuid(), attachment.getId(), attachment.getType());

  webClient.DownloadDataCompleted += (object sender, System.Net.DownloadDataCompletedEventArgs eventArgs) =>
  {
    notificationManager.hidePopup();

    if ((eventArgs.Cancelled == false) && (eventArgs.Error == null))
    {
      byte[] fileBytes = eventArgs.Result;

      Java.IO.File attachmentFile = new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads), attachment.getName());
      System.IO.File.WriteAllBytes(attachmentFile.AbsolutePath, fileBytes);

      // Get MIME type
      String extension = MimeTypeMap.GetFileExtensionFromUrl(attachmentFile.AbsolutePath);
      String mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);

      Intent intent = new Intent(Intent.ActionView);

      Logger.console("File exists: " + attachmentFile.Exists()); // Shows true.

      intent.SetData(Android.Net.Uri.FromFile(attachmentFile));
      intent.SetType(mimeType);

      // Check if any application can open the given MIME type. Otherwise show notice to user
      PackageManager packageManager = Activity.PackageManager;
      IList<ResolveInfo> resolversList = packageManager.QueryIntentActivities(intent, 0);

      if (resolversList.Count > 0)
      {
        Activity.StartActivity(intent);
      }
      else
      {
        Toast.MakeText(Activity, "Unable to find for ext...", ToastLength.Long);
      }

    }
    else if (eventArgs.Error != null)
    {
      Logger.console(eventArgs.Error.Message);
    }
  };

  webClient.DownloadDataAsync(new Uri(attachmentPath));
4

1 回答 1

0

我刚刚找到了这个问题的答案,以防其他人遇到它。我不确定这是否只是一个 Monodroid 错误,或者还有其他我不知道的东西在起作用。要解决此问题,请使用以下两行代码:

intent.SetData(Android.Net.Uri.FromFile(attachmentFile));
intent.SetType(mimeType);

并浓缩于此:

intent.SetDataAndType(Android.Net.Uri.FromFile(attachmentFile), mimeType);

我不敢相信这行得通。在花了将近两天的时间之后,我想只是为了压缩代码,一开始无法弄清楚它为什么开始工作。

于 2013-05-10T08:17:40.047 回答