2016-11-10 38 views
2

我正嘗試將圖像上載到服務器。但是,在執行上載之前,我會檢查圖像大小是否大於2MB。 從活動結果收到圖像後,我運行下面的代碼使用字節數組檢查圖像大小

Bitmap bitmap = MediaStore.Images.Media.GetBitmap(this.Activity.ContentResolver, uri); 
           //int imageSize = 79030; 
           //float size = (bitmap.RowBytes * bitmap.Height)/1024; 
           //float seasonDrop = size/10000; 
           //double actualsizeMb = Math.Round(seasonDrop, 1); 
           // Rotate the image if required. Samsung devices have an Exif property that usually rotates the image. 
     bitmap = rotateImageIfRequired(bitmap, this.Activity, uri); 
     int size = bitmap.ByteCount; 
     var bytesize = bitmap.RowBytes * bitmap.Height; 
     var buffer = Java.Nio.ByteBuffer.Allocate(bytesize); 
     bitmap.CopyPixelsToBuffer(buffer); 
     buffer.Rewind(); 
     var bytes = new byte[bytesize]; 
     float length = bytes.Length/(1024 * 1024); 

我試着上傳1.96MB圖像。但是,當我在長度上放置一個斷點時,我得到的值是10MB。這應該不可能像這樣增加圖像尺寸。

+0

'上傳1.96MB image'到底是什麼1.96 MB? – greenapps

+0

'嘗試將圖像上傳到服務器。您嘗試上傳位圖的所有像素。你沒有告訴位圖來自哪裏。但是,如果你想上傳文件,然後直接上傳這些文件,不要開始搞亂BitmapFactory或Bitmaps。 – greenapps

+0

服務器想要接收什麼?無法解碼的文件或字節流?或者可以嗎? – greenapps

回答

1

1.9 MB可能是壓縮的大小,因爲如果來自相機或圖庫,圖像可能以jpeg或png格式(經過壓縮)。當您檢查位圖大小時,您正在檢查圖像的未壓縮大小,這將比壓縮大小大得多。獲取文件參考時,只需檢查文件的大小,而不將圖像轉換爲位圖。您可以使用.NET FileInfo.Length屬性來檢查圖像文件的壓縮大小。

https://msdn.microsoft.com/en-us/library/system.io.fileinfo.length(v=vs.110).aspx

從上面的鏈接:

// Make a reference to a directory. 
DirectoryInfo di = new DirectoryInfo("c:\\"); 
// Get a reference to each file in that directory. 
FileInfo[] fiArr = di.GetFiles(); 
// Display the names and sizes of the files. 
Console.WriteLine("The directory {0} contains the following files:", di.Name); 
foreach (FileInfo f in fiArr) 
     Console.WriteLine("The size of {0} is {1} bytes.", f.Name, f.Length);