строка в java.io.file - PullRequest
0 голосов
/ 10 июня 2018

Я новичок в Xamarin и пытаюсь открыть файл с устройства.Я получаю эту ошибку

FilePath является строкой, которая содержит путь к файлу.Есть ли способ конвертировать строку в java.io.file в C #.

 var FilePath= Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/newfolder" + "/example.pdf";
 Intent intent = new Intent(Intent.ActionView, Android.Net.Uri.FromFile(filePath));
 intent.SetDataAndType(Android.Net.Uri.FromFile(f), "application/msword");
 StartActivity(intent);

Ответы [ 2 ]

0 голосов
/ 14 июня 2018

Вот мое решение, вы можете скачать отчет с помощью WebClient (То же, что и Android).Отображение пользовательского уведомления для пользователя после загрузки.А когда пользователь нажимает на уведомление, откройте отчет в приложениях устройства по умолчанию (например: .pdf => pdf reader).используйте Java.IO.File, чтобы получить сохраненный отчет URI.

        var ReportAbsolutePath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/Folder";
        Directory.CreateDirectory(ReportAbsolutePath);

        try
        {
            string reportSavedPath = ReportAbsolutePath + "/ReportName.pdf";
            var fileUri = Android.Net.Uri.FromFile(new Java.IO.File(reportSavedPath));

            WebClient webClient = new WebClient();
            webClient.DownloadFileCompleted += (object sender, AsyncCompletedEventArgs e) =>
            {
                Intent intent = new Intent(Intent.ActionView);
                intent.SetDataAndType(fileUri, mimeType);

                var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;

                var notification = new Notification(Resource.Drawable.app_icon, "Title");
                notification.Flags = NotificationFlags.AutoCancel;
                notification.SetLatestEventInfo(this, "title", "desc", 
                PendingIntent.GetActivity(this, 0, mIntent, 0));
                notificationManager.Notify(1, notification);


                ShowToastMessage("Report downloaded successfully.");
            };
            webClient.DownloadFileAsync(new Uri(reportUrl), reportSavedPath);
        }
        catch (Exception ex)
        {
            ShowToastMessage("Error occurred while downloading the report");
        }
0 голосов
/ 13 июня 2018

Есть ли способ конвертировать строку в java.io.file в C #.

Вы можете использовать класс File.

Например:

        var FilePath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/newfolder" + "/example.pdf";
        File file = new File(FilePath);
        Intent intent = new Intent(Intent.ActionView, Android.Net.Uri.FromFile(file)); 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...