Отправить файл в другое приложение из списка приложений в xamarin & android - PullRequest
0 голосов
/ 15 ноября 2018

Я не могу понять, как отправить файл в другие приложения, которые мне нужно отфильтровать из небольшого списка.Я прочитал несколько учебных пособий и ответов (например, Поделиться файлом с другим приложением (WhatsApp, Telegram, Gmail) ) безуспешно:

override this.OnCreate (bundle) =

    base.OnCreate (bundle)
    this.SetContentView (Resources.Layout.Main)

    let button = this.FindViewById<Button>(Resources.Id.myButton)
    button.Click.Add (fun args -> 
        let fileName = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments)
        let fileName = Path.Combine([|fileName; "AboutAssets.txt"|]) |> Path.GetFullPath
        this.Launch(fileName)
    )

member this.Launch(fileName:string) =

    let context = Android.App.Application.Context
    let getUri file =
        Android.Support.V4.Content.FileProvider.GetUriForFile(context, Android.App.Application.Context.PackageName + ".provider", file)

    let file = new Java.IO.File(fileName)
    let path = getUri file
    let extension = Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(path.ToString())
    let fileMime = Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension)

    let intent = new Android.Content.Intent(Android.Content.Intent.ActionSend)
    intent.SetFlags(Android.Content.ActivityFlags.ClearTop) |>  ignore
    intent.SetFlags(Android.Content.ActivityFlags.NewTask) |>  ignore
    intent.SetType("*/*") |> ignore

    let apps = [| "excel"; "mail"; "gmail"; "whatsapp"; "dropbox"; "drive" |]

    let findApps() =
        let packageManager = context.PackageManager
        let resInfo = packageManager.QueryIntentActivities(intent, Android.Content.PM.PackageInfoFlags.Activities)

        let targetedShareIntents = new ResizeArray<IParcelable>()

        let pkgs = seq {
            for info in resInfo do
                let namePkg = info.ActivityInfo.PackageName.ToLower()
                let nameApp = info.ActivityInfo.Name.ToLower()

                let label = info.LoadLabel(packageManager)
                let icon = info.Icon;


                for app in apps do
                    if nameApp.Contains(app) || namePkg.Contains(app) then
                        yield (app, info)
        }

        for (app, info) in pkgs |> Seq.distinct do
            let label = info.LoadLabel(packageManager)
            let icon = info.Icon;

            let targetedShare = new Android.Content.Intent(Android.Content.Intent.ActionSend)

            if app.Contains("email") then
                targetedShare.SetType("message/rfc822") |> ignore //not work
            else
                targetedShare.SetType(fileMime) |> ignore
            targetedShare.SetType(fileMime) |>  ignore

            targetedShare.PutExtra(Android.Content.Intent.ExtraText, "My body of post/email") |>  ignore
            targetedShare.AddFlags(Android.Content.ActivityFlags.GrantReadUriPermission) |>  ignore

            targetedShare.PutExtra(Android.Content.Intent.ExtraStream, path) |> ignore
            targetedShare.SetData(path)|>  ignore
            targetedShare.SetFlags(Android.Content.ActivityFlags.ClearTop) |>  ignore
            targetedShare.SetFlags(Android.Content.ActivityFlags.NewTask) |>  ignore

            //targetedShare.SetData(uri) |> ignore
            let label = new Android.Content.PM.LabeledIntent(targetedShare, info.ActivityInfo.PackageName, label, icon)
            targetedShareIntents.Add(label) |>  ignore

        targetedShareIntents

    let apps = findApps()
    printf "%A" apps

Android.Content.Intent (Android.Content.Intent.ExtraInitialIntents, apps)

    let chooser = Android.Content.Intent.CreateChooser(intent, "Choose App")
    chooser.PutExtra(Android.Content.Intent.ExtraInitialIntents, apps.ToArray()) |> ignore

    chooser.SetFlags(Android.Content.ActivityFlags.ClearTop)|> ignore
    chooser.SetFlags(Android.Content.ActivityFlags.NewTask) |> ignore           

    context.StartActivity(chooser)

Когда я открываю Gmail, файл не отображается, и не отображается ошибка.При открытии Google Диска появляется сообщение «Загрузка не была успешной, запрос не содержит данных»

PD: Конфигурация:

<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.companyname.basura.provider" android:exported="false" android:grantUriPermissions="true">
 <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" />
</provider>

И имя_производителя:

<?xml version="1.0" encoding="UTF-8" ?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
  <files-path name="files" path="."/>
</paths>
...