Xamarin.Android实现分享文件到微信


  var dbFile = Path.Combine(DBSetting.GetSetting().DBDirectory, $"{BLL.SelectProject.DBName}.db");
                        var sharefile = new Xamarin.Essentials.ShareFile(dbFile)
                        {
                            ContentType = "application/db"
                        };
                        await Share.RequestAsync(new ShareFileRequest
                        {
                            Title = $"分享项目文件{BLL.SelectProject.DBName}.db",
                            File = sharefile
                        });

使用即可用第三方应用打开。

Share.RequestAsync

接收微信分享的文件,在

   protected override void OnResume()
   {
            base.OnResume();
            this.SaveFile(Intent);
   }

 public static PYLMath.CommonExecuteResult<string> SaveFile(this Activity content, Intent Intent)
        {
            var res = new PYLMath.CommonExecuteResult<string>();
            //使用微信第三方应用打开文件时
            var extras = Intent.Extras;
            if (extras == null)
            {
                res.IsSuccess = false;
                return res;
            }
            string tempPath;
            try
            {
                tempPath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "temp");
                if (!System.IO.Directory.Exists(tempPath))
                {
                    System.IO.Directory.CreateDirectory(tempPath);
                }
            }
            catch (Exception ex)
            {
                res.InnerException = ex;
                return res;
            }
            //
            string action = Intent.Action; //如果从微信中点击【使用第三方应用打开】,就是ActionView
                                           //如果微信分享的是*.db,那么 "application/db" ;如果是*.db..octet-stream,那么就application/octet-stream
            string type = Intent.Type;
            var uri = Intent.Data;
            int readCount = 0;
            char[] buffer = null;
            //创建一个请求
            try
            {
                //参考:https://segmentfault.com/a/1190000021357383
                ParcelFileDescriptor parcelFileDescriptor = content.ContentResolver.OpenFileDescriptor(Intent.Data, "r");
                var reader = new Java.IO.FileReader(parcelFileDescriptor.FileDescriptor);
                var size = parcelFileDescriptor.StatSize;
                if (reader.Ready() == false)
                {
                    return res;
                }
                buffer = new char[size];
                readCount = reader.Read(buffer, 0, (int)size);
                //
                parcelFileDescriptor.Close();
                reader.Close();
            }
            catch (Java.IO.FileNotFoundException e)
            {
                res.InnerException = e;
                res.IsSuccess = false;
                return res;
            }
            catch (Exception ex)
            {
                res.InnerException = ex;
                res.IsSuccess = false;
                return res;
            }
            //
            if (readCount <= 0)
            {
                return res;
            } 
            byte[] bytes = new byte[readCount];
            for (int i = 0; i < readCount; i++)
            {
                bytes[i] = (byte)buffer[i];
            }
            try
            {
                //
                var fileName = System.IO.Path.GetFileName(Nancy.Helpers.HttpUtility.UrlDecode(Intent.DataString, System.Text.Encoding.UTF8));
                var saveFile = System.IO.Path.Combine(tempPath, fileName);
                System.IO.File.WriteAllBytes(saveFile, bytes);
                //
                res.Content = saveFile;
                res.IsSuccess = true;
                return res;
            }
            catch (Exception ex)
            {
                res.InnerException = ex;
                return res;
            }
        }