我不知道如何发送文件以在其他应用程序中共享,我需要从文件的一小部分进行过滤。我已经阅读了一些教程和答案(例如Share file to another app (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,应用程序)
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云端硬盘时,提示“上传不成功,请求中没有数据”
P.D:配置:
<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>
和provider_paths:
<?xml version="1.0" encoding="UTF-8" ?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="files" path="."/>
</paths>