سلام من از اینتت برای انتخاب عکس از گالری یا دوربین استفاده کردم و مشکلی نداره فقط چون ممکن هست عکسی که از طریق دوربین یا گالری انتخاب میشه حجمش زیاد باشه راهی هست که در اندروید حجم مشخصی در نظر بگیرم یعنی اگر بیشتر از اندازه خاصی باشه یا پیغام بده و یا اینکه خود برنامه سایزش رو کم کنه؟
با کد زیر میتونید تصاویر را تغییر سایز دهید و حجمشون رو کم کنید
public void ResizeImage(string sourceFile, string targetFile, float maxWidth, float maxHeight) { if (!File.Exists(targetFile) && File.Exists(sourceFile)) { // First decode with inJustDecodeBounds=true to check dimensions var options = new BitmapFactory.Options() { InJustDecodeBounds = false, InPurgeable = true, }; using (var image = BitmapFactory.DecodeFile(sourceFile, options)) { if (image != null) { var sourceSize = new Size((int)image.GetBitmapInfo().Height, (int)image.GetBitmapInfo().Width); var maxResizeFactor = Math.Min(maxWidth / sourceSize.Width, maxHeight / sourceSize.Height); string targetDir = System.IO.Path.GetDirectoryName(targetFile); if (!Directory.Exists(targetDir)) Directory.CreateDirectory(targetDir); if (maxResizeFactor > 0.9) { File.Copy(sourceFile, targetFile); } else { var width = (int)(maxResizeFactor * sourceSize.Width); var height = (int)(maxResizeFactor * sourceSize.Height); using (var bitmapScaled = Bitmap.CreateScaledBitmap(image, height, width, true)) { using (Stream outStream = File.Create(targetFile)) { if (targetFile.ToLower().EndsWith("png")) bitmapScaled.Compress(Bitmap.CompressFormat.Png, 100, outStream); else bitmapScaled.Compress(Bitmap.CompressFormat.Jpeg, 95, outStream); } bitmapScaled.Recycle(); } } image.Recycle(); } else Log.E("Image scaling failed: " + sourceFile); } } }
ببخشید من این تابع رو اضافه کردم ولی خطا میده ممکنه یکم راهنمایی بکنید خیلی لازم دارم
سلام من فکر می کنم این تابع برای زمارین جواب میده توی محیط اندروید استادیو خطا میده مثلا var رو نمیشناسه
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)