我正在尝试让外部应用程序打开在运行时确定的不同文件类型。从我看过的所有内容来看,我觉得我做得对,但没有一个程序正确打开文件。对于使用文本文件的Astro,它显示以下错误:java.lang.illegalargumentexception用于观察者的uri无效。基本上它似乎没有得到正确的文件位置。任何帮助将不胜感激。
这是我的代码:
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));
答案 0 :(得分:0)
我刚刚找到了这个问题的答案,以防其他人遇到它。我不确定这只是一个Monodroid的bug还是还有其他一些我不知道的东西。要解决此问题,请使用以下两行代码:
intent.SetData(Android.Net.Uri.FromFile(attachmentFile));
intent.SetType(mimeType);
并缩写为:
intent.SetDataAndType(Android.Net.Uri.FromFile(attachmentFile), mimeType);
我无法相信这有效。花了差不多两天的时间后,我想只是为了压缩代码而无法弄清楚为什么它开始工作。