我试图让外部应用程序打开在运行时确定的不同文件类型。从我所看到的一切来看,我觉得我做得对,但没有一个程序能正确打开文件。对于使用文本文件的 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));