Forum Xamarin.Android

Handle Intents with attached file

RogerSchmidlinRogerSchmidlin CHUniversity ✭✭✭

Hello there

I would like my Xamarin Forms App to handle specific files that are passed in via intents through file explorer or web browser. I am facing a few problems:

  • I have no clue how to handle the received intent. I tried some java examples, but they don't map with C#. Does anyone have sample code to handle received files?
  • I have implemented the filter to receive but also get called when receiving emails, even though I specifically registered for files with *.wst
  • when file explorer app calls my app with the intent, an exception is called.

Unable to instantiate activity ComponentInfo{WhatStep.SenseMakingApps/WhatStep.SenseMakingApps.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "WhatStep.SenseMakingApps.MainActivity" on path: DexPathList[[zip file "/data/app/WhatStep.SenseMakingApps-1/base.apk"],nativeLibraryDirectories=[/data/app/WhatStep.SenseMakingApps-1/lib/arm, /vendor/lib, /system/lib]]

`









Java code to handle intent with file attachment -> Code in C#?
Intent intent = getIntent();
String action = intent.getAction();
if (action != null && action.equals(Intent.ACTION_VIEW)) {
txtFilePath = intent.getData().getPath();
}
`

Tagged:

Answers

  • RogerSchmidlinRogerSchmidlin CHUniversity ✭✭✭

    Solution to the class not found error: Xamarin wraps the java activities and generates some md5 number infront of the actual activity for the final AndroidManifest.xml. In order for android to find your activity you must define your intend filters in the YourActivity.cs file itself like this and don't touch the Manifest at all:

    `
    [Activity (Label = "WhatStep", Icon = "@drawable/icon", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, LaunchMode = LaunchMode.SingleInstance)]
    [IntentFilter (new[]{Intent.ActionView}, Categories=new[]{Intent.CategorySampleCode, "android.intent.category.DEFAULT"}, DataMimeType = "/", DataPathPattern = ".*\.wst")]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity

    `

  • RogerSchmidlinRogerSchmidlin CHUniversity ✭✭✭

    You can only have one activity to receive intents. My app has a splash screen (another activity) before my MainActivity gets called. Because my splash screen gets the MainLauncher=True my MainActivity doesn't. And that means you can't handle intents anymore in your activity.
    Apparently Xamarin doesn't support this according to a bug I reported and closed it.
    https://bugzilla.xamarin.com/show_bug.cgi?id=31087
    I still think it's a bug within Xamarin.

Sign In or Register to comment.