我正在尝试将图片上传到服务器。但是,在执行上传之前,我会检查图像大小是否大于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)
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);