如何编写一个 MAUI 应用程序来接收来自另一个应用程序的文件?

问题描述 投票:0回答:1

我是 MAUI 的新手,我需要我的 MAUI 应用程序接收从 Android 上其他应用程序共享的 json 文件。我搜索了很多但一点运气都没有。没有找到指南工作。

我尝试了一个专门的活动类,然后尝试将这些代码移到 MainActivity 中。都不起作用。

这是我在我的应用程序上所做的:

在 AndroidManifest.xml 中

  <application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true" android:label="KitCare">
    <activity android:name="KitCare.DataFileIntentActivity" android:exported="true">
      <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="application/json" />
      </intent-filter>
    </activity>
  </application>

在DataFileIntentActivity.cs中:

namespace KitCare
{
    [Activity(Name = "KitCare.DataFileIntentActivity", Exported = true),
    Theme = "@style/MyAppTheme" //Requested by Jessie Zhang. Theme name is the same as the main activity.
    ]
    [IntentFilter(
        new[] { Android.Content.Intent.ActionSend },
        Categories = new[] { Android.Content.Intent.CategoryDefault },
        DataMimeType = "application/json")]
    public class DataFileIntentActivity : MauiAppCompatActivity
    {
        protected override void OnCreate(Bundle? savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            //Requested by Jessie Zhang
            Platform.Init(this, savedInstanceState);
            OnNewIntent(Intent);
        }

        protected override void OnNewIntent(Intent? intent)
        {
            base.OnNewIntent(intent);

            if (Intent?.Action == Intent.ActionSend)
            {
                Stream? inputStream = null;

                var filePath = Intent?.ClipData?.GetItemAt(0);
                if (filePath?.Uri != null)
                {
                    inputStream = ContentResolver!.OpenInputStream(filePath.Uri)!;
                }
                
                if (inputStream != null)
                {
                    using (var reader = new StreamReader(inputStream))
                    {
                        var content = reader.ReadToEnd();

                        //process the content here...
                    }

                    inputStream.Close();
                    inputStream.Dispose();
                }
            }
        }
    }
}

Currently, I can see my app listed as a target when a file is selected for sharing.但是在选择应用程序后,会显示一个带有程序标题的空 UI,并立即退出。

有什么想法吗? 非常感谢。

android-intent share maui
1个回答
0
投票

您可以尝试将您的代码添加到功能

OnCreate
.

请参考以下代码:

   [Activity(Label = "FormApp", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, Exported = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] 
    [IntentFilter
(
   new[] { Android.Content.Intent.ActionView, Android.Content.Intent.ActionSend },
   Categories = new[]
       {
             Android.Content.Intent.CategoryDefault
       },

   DataMimeType = "application/json"
)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
    
         // add your code here
            if (Intent?.Action == Intent.ActionSend)
            {
                Stream? inputStream = null;
                var filePath = Intent?.ClipData?.GetItemAt(0);
                if (filePath?.Uri != null)
                {
                    inputStream = ContentResolver!.OpenInputStream(filePath.Uri)!;
                }

                if (inputStream != null)
                {
                    using (var reader = new StreamReader(inputStream))
                    {
                        var content = reader.ReadToEnd();

                        //process the content here...
                    }

                    inputStream.Close();
                    inputStream.Dispose();
                }
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.