0
我試圖讓外部應用程序打開運行時確定的不同文件類型。從我看過的所有內容中,我感覺我做得很對,但沒有一個程序能正確打開文件。對於使用文本文件的Astro,它會顯示以下錯誤:java.lang.illegalargumentexception無效的uri用於觀察者。基本上它似乎沒有得到正確的文件位置。任何幫助將不勝感激。使用Monodroid的Android應用程序無法獲得外部程序來正確打開文件
這裏是我的代碼:
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));