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
                        });

接收微信分享的文件

在MainActivity.cs中

  //参考:https://blog.csdn.net/j5856004/article/details/102651886
    //参考:https://www.jianshu.com/p/2f2ffb6ec4bb
    //必须要单独写这个IntentFilter,否则桌面就没有图标了!!!!!重点中的重点
    [IntentFilter(new[] { Intent.ActionMain }, Categories = new[] { Intent.CategoryLauncher })]
    [IntentFilter(
        new[] { Intent.ActionView, Intent.ActionSend },// Intent.ActionView ,确保微信点击【使用第三方应用打开】时可以看到图标
        Categories = new[] { Intent.CategoryDefault },
        /*DataScheme = "file",*/ //如果QQ也要打开,这里要设置file
                                 //DataMimeType确保特定的文件,可以使用此APP打开
        /*DataMimeType = "application/octet-stream"*/ //设置了application/octet-stream,就可以接.db.octet-stream文件
        DataMimeType = "text/comma-separated-values" //*/*表示接收所有文件
        )]
    //这个是项目默认的,不用改
    [Activity(Label = "FmosMobile", Icon = "@mipmap/fmos", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {

     }

 在MainActivity的OnResume方法重写中,加入:

   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;
            }
        }

 打包的一些注意事项

一、去掉钩

 

 二、存档

 

 

 

 

 

 

 重点:这个一定要保存好,所谓【证书/签名/密钥】等等,就是这个东西。

如果每次不是用同一个【证书】,那么,在更新的时候,就会出现”证书不一致“,导致应用必须要卸载才能更新,最终后果就是,APP本地数据全部丢失!!!!

还有,com.xxxxx.xxx.apk,中间的xxxxx.xxx一定要写好,在项目属性那里设置,一旦发布了被客户使用了,就没得后悔了,不然又得卸了重装,不能通过升级解决!!!

 

 

保存为APK

 

 其他小坑: 

一、

 切换成release才可以分发

 

二、为了避免安装的时候,手机显示没有证书,最好给客户这个apk。(反正我试过没有什么问题,包括安装、升级啥的,或许不这样也行)

 

 

三、不要乱升级Xamarin.Form,默认是5.0.0.2012版,否则会出现javac.exe错误,无解!!!!

四、遇到调试时闪退,不报任何错误,不要打勾

 

五、请在运行应用程序之前选择有效的设备 。

(一)重装ADB:Win10 配置安装ADB教程总结20200514 - 知乎 (zhihu.com)

六、XABLD7000: Xamarin.Tools.Zip.ZipException: Renaming temporary file failed: Permission denied

 (一)切换成release模式

 (二)不要打勾

 

 (三)重新生成、部署

 (四)切换回DEBUG模式

 

七:使用Jar包:绑定 .JAR - Xamarin | Microsoft Docs

八:使用aar包:用解压工具加压后,得到jar包

九:一些很难用的jar包,使用VsCode,再用java包装一下导出jar包用。

 

 

 

导出的问题:xamarin Unsupported class file major version 61

解决方案:修改编译使用的JDK版本

 

原本是17的,改成11。

 

十:出现GSON错误,在管理NUGET包中,添加GoogleGson包。

 

文章出处:https://www.cnblogs.com/pylblog/p/15802525.html

版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
管理员
上一篇:使用.NET 6开发TodoList应用(29)——实现静态字符串本地化功能
下一篇:c#对于加密的一点整合 (AES,MD5,RSA,SHA256)
评论列表

发表评论

评论内容
昵称:
关联文章

Xamarin.Android
Xamarin.Forms 复制本地SQLite数据库
ASP.NET+MVC入门笔记 (一) 创建项目 项目配置运行 以及简单的Api搭建
一次 .NET 某智能交通后台服务 CPU爆高分析
一次 .NET 某药品仓储管理系统 卡死分析
.NET 微服务——CI/CD(4):避和一点经验
一次本地正常上线接口报404
使用.NET 6开发TodoList应用(填1)——实现当前登录用户获取
PDA android获得屏幕设计尺寸
一次.Net Core程序启动失败的排查过程
一次 WinDbg 分析 .NET 某工厂MES系统 内存泄漏分析
一次数据库查询优化记录
一次 .NET 某妇产医院 WPF内存溢出分析
一次 .NET 某化妆品 webapi 卡死分析
一次 .NET 某消防物联网 后台服务 内存泄漏分析
一次 .NET 某市附属医院 Web程序 偶发性CPU爆高分析
【已解决】.NET 微信支付API V3中JSAPI支付发起wx.chooseWXPay时,提示 支付验证签名失败
.NET 微服务——CI/CD(3):镜像自动分发
微信支付:签名计算.net4.5
selenium爬虫被检测到 该如何破

联系我们
联系电话:15090125178(微信同号)
电子邮箱:garson_zhang@163.com
站长微信二维码
微信二维码