您现在的位置是:首页 > 文章详情

Xamarin.Android 压缩图片并上传到WebServices

日期:2018-04-24点击:673

随着手机的拍照像素越来越高,导致图片赞的容量越来越大,如果上传多张图片不进行压缩、质量处理很容易出现OOM内存泄漏问题。

最近做了一个项目,向webservices上传多张照片,但是项目部署出来就会出现闪退现象,后来经行调试发现图片没有进行压缩,一张图片大小为2M,然而webservices没法接搜多个大图片,所以需要改下配置文件,我这里改为40M。

 <system.web> <httpRuntime maxRequestLength = "40960" useFullyQualifiedRedirectUrl="true"/> </system.web>

 这里改好后发现上传图片还是有问题,后来经过一步步调试发现将本地图片转换成Bitmap后没有清空,然后一直存放在内存中,导致内存泄漏。只要把转换完的Bitmap清空一下就好了。

 /// <summary> /// 图片转换成String流 /// </summary> /// <param name="file_path">文件名(不带file://)</param> /// <returns></returns> public static string ImageToString(string file_path) { //待上传图片路径 //string uploadFile = file_path; //转化成文件 //System.IO.FileInfo imgFile = new System.IO.FileInfo(uploadFile); ////文件转化成字节 //byte[] imgByte = new byte[imgFile.Length]; //////读文件  //System.IO.FileStream imgStream = imgFile.OpenRead(); //////文件写入到字节数组 //imgStream.Read(imgByte, 0, Convert.ToInt32(imgFile.Length)); //////字节数组转换成String类型 //string by = Convert.ToBase64String(imgByte); ////上传到服务器 后面是文件名 ////fileUp.UpdateFile(imgByte, Guid.NewGuid() + ".png"); //return imgByte;   Bitmap bitmap = BitmapFactory.DecodeFile(file_path); //将图片文件转换成bitmap 格式:/storage/emulated/0/DCIM/Camera/IMG_20180425_105725.jpg string bitstring = BitmapToString(bitmap); bitmap = null; //一定要清空,否则会导致OOM问题  GC.Collect(); return bitstring; } /// <summary> /// 图片缩放处理 /// </summary> /// <param name="bgimage">Bitmap文件</param> /// <param name="newWidth">新图片宽度</param> /// <param name="newHeight">新图片高度</param> /// <returns></returns> public static Bitmap zoomImage(Bitmap bgimage, double newWidth, double newHeight) { // 获取这个图片的宽和高 float width = bgimage.Width; float height = bgimage.Height; // 创建操作图片用的matrix对象 Matrix matrix = new Matrix(); // 计算宽高缩放率 float scaleWidth = ((float)newWidth) / width; float scaleHeight = ((float)newHeight) / height; // 缩放图片动作  matrix.PostScale(scaleWidth, scaleHeight); Bitmap bitmap = Bitmap.CreateBitmap(bgimage, 0, 0, (int)width, (int)height, matrix, true); return bitmap; } static string BitmapToString(Bitmap bitmap) { Bitmap bit = zoomImage(bitmap, 750, 1000);//小图 //质量压缩 //MemoryStream stream = new MemoryStream(); //bit.Compress(Bitmap.CompressFormat.Jpeg, 50, stream); //byte[] bitmapData = stream.ToArray(); //Bitmap map = BitmapFactory.DecodeByteArray(bitmapData, 0, bitmapData.Length); //btn_imagetwo.SetImageBitmap(map); //Bitmap im = zoomImage(bitmap, 800, 900);//大图 MemoryStream big_stream = new MemoryStream(); bit.Compress(Bitmap.CompressFormat.Jpeg, 80, big_stream); byte[] big_bitmapData = big_stream.ToArray(); return Convert.ToBase64String(big_bitmapData); } 

webservices接受进行保存图片:

 private String ImagePath = "/HandlerImages/"; /// <summary> /// 上传图片 /// </summary> /// <param name="content">图片字符流</param> /// <param name="pathandname">图片名称</param> /// <returns></returns>  [WebMethod] public bool UpdateFile(string content, string pathandname) { //保存图片路径 string FilePath = Server.MapPath(ImagePath); //判断路径是否存在 if (!Directory.Exists(FilePath)) { //创建路径  Directory.CreateDirectory(FilePath); } string SaveFilePath = Path.Combine(FilePath, pathandname); byte[] fileBytes; try { fileBytes = Convert.FromBase64String(content); MemoryStream memoryStream = new MemoryStream(fileBytes); //1.定义并实例化一个内存流,以存放提交上来的字节数组。  FileStream fileUpload = new FileStream(SaveFilePath, FileMode.Create); ///2.定义实际文件对象,保存上载的文件。  memoryStream.WriteTo(fileUpload); ///3.把内存流里的数据写入物理文件   memoryStream.Close(); fileUpload.Close(); fileUpload = null; memoryStream = null; return true; } catch { return false; } }

调用webservices上传图片:

MyWebService service = new MyWebService(); 
service.UpdateFile(ImageToByte("/storage/emulated/0/DCIM/Camera/IMG_20180425_105725.jpg"),Guid.NewGuid().ToString() + ".jpg");

 

原文链接:https://yq.aliyun.com/articles/618736
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章