0

在我的安卓应用中。我正在尝试将图像上传到我的服务器。我需要文件大小小于 1mb。如果我有 imageview 对象,我如何以编程方式检查该大小是否小于 1mb?

谢谢

4

1 回答 1

0
    BitmapDrawable bmpDrawable = imgView.getDrawable();
          Bitmap bmp          = bmpDrawable.getBitmap();

ByteArrayOutputStream out = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormatJPEG, 100, out);

int size = out.count;

if( out.count > 1024 *1024 * 1024 )
{
       // Greater than 1 MB
}

// 使用此位图通过 BitmapFacotry.Options 类获取大小 // 大小取决于您如何压缩图像和发送,但您可以通过获取图像宽度和大小并计算使用的像素来了解。因为 jpeg 大小因图像而异,因此使用 IO Stream 将文件写入为 JPEG,然后查看其中的字节数。

于 2013-07-21T23:16:44.037 回答