【问题标题】:How to get sharing PDF file's path and open that in our app?如何获取共享 PDF 文件的路径并在我们的应用程序中打开它?
【发布时间】:2019-11-14 03:48:10
【问题描述】:

有一个问题我很久没有解决。我想使用共享按钮从另一个应用程序到我用 xamarin 创建的应用程序获取 PDF 文件的路径。我已经尝试了一些东西,那些解决了我的问题,但这不是我想要的。那是;我将pdf下载到手机存储中。所以我的手机上有很多pdf。之后,我可以与我的应用程序共享这些 pdf,我可以在那里看到它们。但这并不是我想要的。假设有一个包含大量 pdf 的 ABC 应用程序。我想打开这些 pdf 文件并直接与我的 Xamarin 应用程序共享而不存储它们。我可以用一个例子来解释这一点:你有一个 PDF,你想与你的朋友分享。您打开该 Pdf 并单击“共享”按钮。之后,您将选择要使用的应用程序,例如 Whatsapp、电子邮件、Skype、Discord 等。因此,您最后可以将您的 pdf 直接发送到另一个应用程序而无需存储。

 Intent intent = new Intent(Application.Context, typeof(MainActivity));



        var action = Intent.Action;
        var type = Intent.Type;

        if (Android.Content.Intent.ActionSend.Equals(action) &&
            (type?.Equals("text/plain") ?? false))
        {
            var path = Intent.GetStringExtra(Android.Content.Intent.ExtraText);
            Console.WriteLine(path);
        }


        if (Android.Content.Intent.ActionSend.Equals(action) && (type?.Equals("application/pdf") ?? false))
        {
            var uri = (Android.Net.Uri)Intent.GetParcelableExtra(Android.Content.Intent.ExtraStream);
            Context context = Android.App.Application.Context;
            var fullPath = GetFilePath(uri);               
            Navigate(fullPath);

        }
private string GetFilePath(Android.Net.Uri uri)
        {

            string[] proj = { MediaStore.Images.ImageColumns.Data };
            var cursor = ContentResolver.Query(uri, proj, null, null, null);            
            var colIndex = cursor.GetColumnIndex(MediaStore.Images.ImageColumns.Data);
            cursor.MoveToFirst();
            return cursor.GetString(colIndex);            

        }

这是我将 pdf 共享到我的应用时的错误:

未处理的异常:Java.Lang.IllegalStateException:无法从 CursorWindow 读取第 0 行 col -1。确保 Cursor 在从中访问数据之前已正确初始化。

【问题讨论】:

  • 那么现在的问题是什么?上面的代码不能接收数据吗?
  • 代码获取数据,但如果手机存储中有数据。实际上我想允许其他应用程序启动我的活动和我的应用程序。当我将另一个应用程序的 pdf 共享到我的应用程序时,它会出现错误像这样:未处理的异常:Java.Lang.IllegalStateException:无法从 CursorWindow 读取第 0 行,列 -1。确保在从光标访问数据之前正确初始化光标。
  • 也许你可以试试MediaStore.Files
  • 我已经尝试过,但我又遇到了同样的错误。谢谢你的帮助。我想我需要尝试一些不同的东西。

标签: c# android pdf xamarin xamarin.forms


【解决方案1】:

我已经从这个网站更改了我的代码:

https://codemilltech.com/sending-files-to-a-xamarin-forms-app-part-2-android/

它有效。希望它可以帮助某人。

if (Android.Content.Intent.ActionSend.Equals(action) && (type?.Equals("application/pdf") ?? false))
                {
                    // This is just an example of the data stored in the extras 
                    var uriFromExtras = Intent.GetParcelableExtra(Intent.ExtraStream) as Android.Net.Uri;
                    var subject = Intent.GetStringExtra(Intent.ExtraSubject);

                    // Get the info from ClipData 
                    var pdf = Intent.ClipData.GetItemAt(0);

                    // Open a stream from the URI 
                    var pdfStream = ContentResolver.OpenInputStream(pdf.Uri);

                    // Save it over 
                    var memOfPdf = new System.IO.MemoryStream();
                    pdfStream.CopyTo(memOfPdf);
                    var docsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
                    var filePath = System.IO.Path.Combine(docsPath, "temp.pdf");

                    System.IO.File.WriteAllBytes(filePath, memOfPdf.ToArray());

                    Navigate(filePath);

                }

【讨论】:

    猜你喜欢
    • 2023-01-29
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 2014-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多