將數據發送給微信
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" //*/*表示接收所有文件
)]
//以下這個IntentFilter,能確保其他APP,接收到文件后,進行分享時,能看到這個APP的圖標!!!!,非常重要!!
[IntentFilter(
new[] { Intent.ActionView, Intent.ActionSend },
Categories = new[] { Intent.CategoryDefault },
DataScheme = "file",
DataMimeType = "*/*"
)]
//這個是項目默認的,不用改
[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包。
遠程調試
在安裝了ADB.exe的情況下,連上USB線
調試的時候,選擇
使用ADB安裝apk包。(注意只能連一台設備)