BlackShawarna
BlackShawarna

Reputation: 49

xamarin form android need to open pdf with my app (no show)

I'm using this guide to open a pdf inside my app. All I want is to see my app in the list of apps that can be aple to open a file (pdf) when I press 'share'. So, until now my iOS project works so it's an Android settings problem.

MainActivity:

[Activity(Label = "Test", Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = true)]
[IntentFilter(
    new[] { Intent.ActionView },
   Categories = new[]
  {
      Intent.CategoryDefault,
      Intent.CategoryBrowsable,
  },
  DataScheme = "file",
  DataMimeType = "*/*",
  DataPathPattern = ".*\\.pdf"
)]

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(bundle);

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

        global::Xamarin.Forms.Forms.Init(this, bundle);

        var application = new App();

        if (Intent.ActionView.Equals(action) && !String.IsNullOrEmpty(type))
        {
            Android.Net.Uri fileUri = Intent.Data;

            if (fileUri != null)
            {
                var fileContent = File.ReadAllBytes(fileUri.Path);
                var name = fileUri.LastPathSegment;
                application.SetExternDocument(new IncomingFile()
                {
                    Name = name,
                    Content = fileContent
                });
            }
        }

        LoadApplication(application);
    }
}

and Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
android:versionCode="1" 
android:versionName="1.0" 
package="com.bbinternational.inviofatture.BBInvioFatture">

<uses-sdk android:minSdkVersion="21" />

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />

As you can see I didn't write anything into the manifest because I used Attributes in the main Activity. I tried to use manifest but i can't retrieve the MainActivity android:name property using Xamarin (at runtime it allocates some random values or at least is what I know).

Thanks for the help.

Cheers guys.

Upvotes: 0

Views: 649

Answers (1)

BlackShawarna
BlackShawarna

Reputation: 49

So, after hours of googling this dude saved me.

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

As i supposed the IntentFilterAttributes were wrong. This is the right version

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

Hope to help somebody.

Upvotes: 1

Related Questions