Reputation: 23
There is a problem I couldn't solve for a long time. I want to get a PDF file's path with the share button from another application to my application which I created with xamarin. I have tried somethings and those solved my problem but that wasn't like I wanted. That was; I downloaded the pdf to my phone storage. So there was a lot of pdf on my phone. After that, I can share those pdf with my app and I can see them in there. But that wasn't exactly what I want. Let's say there is an ABC application which has a lot of pdf. I want to open those pdf and share with my Xamarin App directly without storage them. I can explain this with an example: You have a PDF which you want to share with your friends. You open that Pdf and click the Share button. After that, you'll select which application you want to use like Whatsapp, E-Mail, Skype, Discord, etc. So, at last, you can send your pdf directly to another App without store.
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);
}
This is the error when I share pdf to my app:
Unhandled Exception: Java.Lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
Upvotes: 0
Views: 820
Reputation: 23
I have changed my code like this from this web site:
https://codemilltech.com/sending-files-to-a-xamarin-forms-app-part-2-android/
and it works. Hope it helps someone.
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);
}
Upvotes: 1