【问题标题】:Android Load image to image view from gallery without reduce image sizeAndroid将图像从图库加载到图像视图而不减小图像大小
【发布时间】:2020-05-01 01:10:25
【问题描述】:

我使用以下代码将图库图像导入到我的ImageView,但它会减小图像大小。如果原始图片大小在5MB以上,导入保存图片后,图片大小缩小到2MB,质量下降。对于从图库中获取图像并在不降低尺寸和质量的情况下进行保存有什么建议吗?

Bitmap bitmapImage = BitmapFactory.decodeFile(images.get(position));
setBitmap(bitmapImage);

【问题讨论】:

  • 你是如何保存图像的。分享代码
  • FileOutputStream out = new FileOutputStream(file, false); ` saveBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);` out.flush(); out.close(); MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
  • 我在从图库导入时调试我的代码,位图大小为 2100、1080。但检查了我的手机和原始分辨率大小为 4200、2800 之类的......我认为导入时间会减少大小。我说的对吗?
  • 您在谈论图像大小而不是文件大小。然后你提到决议。因此,您正在某处调整原始文件的大小。请在您的帖子中发布可重现的完整代码。不在 cmets 中。厘米。是给我们的。我们与images.get(position) 没有任何关系。最好给出实际值。

标签: android


【解决方案1】:

由于压缩格式和比例因素,大小正在发生变化。你的方法有一个小问题。

Bitmap bitmapImage = BitmapFactory.decodeFile(images.get(position));
setBitmap(bitmapImage);

我会鼓励你检查位图的大小,使用方法 bitmap.getByteCount() 。您可以观察大小的差异,不要感到困惑。这是因为压缩。 (PNG、JPG实际上是以压缩的形式存储图像。

您应该使用 BitmapFactor.Options 来加载与位图相同的大小。

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bitmapImage = BitmapFactory.decodeFile(images.get(position), options);

您可以看到我们通过设置 inJustDecodeBounds=true 配置了我们的 option。它提供有关图像的信息(宽度、高度等)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-26
    • 1970-01-01
    • 2013-03-25
    • 1970-01-01
    • 2013-01-01
    • 2012-08-09
    • 1970-01-01
    相关资源
    最近更新 更多