Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to get Stream or Path from Intent on MainActivity.

I'm junior developer xamarin and I can use English a little bit. I need someone for help me about this.
I want to create application to default app for PDF. When open the PDF file, it will show my application to open PDF.

I add this code on MainActivity.cs

[IntentFilter( new [] { Intent.ActionView }, 
     Categories = new [] { Intent.CategoryDefault, Intent.CategoryBrowsable }, 
     DataMimeType = "application/pdf" )]

I create static void OpenPDF(Intent intent) on MainActivity.cs and call from OnCreate and OnNewIntent

protected override void OnCreate (Bundle bundle) 
{    
     base.OnCreate(bundle);
     .
     .
     .
     OpenPDF(Intent);
 }
protected override void OnNewIntent(Intent intent)
{
    base.OnNewIntent(intent);

    OpenPDF(intent);
}

I want to get Stream or Path on this.

private static void OpenPDF(Intent intent)
{
     string action = intent?.Action;
     string type = intent?.Type;

     if (Intent.ActionView.Equals(action) && (type?.Equals("application/pdf") ?? false))
     {
                ///Code for get Stream or Path.
     }
}

Thank you for the answer in advance.

Best Answer

Answers

  • yurno_xyurno_x Member

    @YelinZh said:
    What 'intent' did you pass to the 'OpenPDF' method? Try to use the Activity.Intent command to get the intent which started the Acitivty, and you could get the data from it.

    Check the code:

    Intent fileIntent;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        ...
        fileIntent = this.Intent;
        OpenPDF();
    }
    protected override void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);
        OpenPDF();
    }
    
    private static void OpenPDF()
    {
        if (Intent.ActionView.Equals(action) && (type?.Equals("application/pdf") ?? false))
        {
            Android.Net.Uri uri = intent.Data;
            string file_path = uri.Path;
    
            //read the file from the path to get the stream
        }
    }
    

    Thanks for the answer. You can help me a lot.

Sign In or Register to comment.