gpt4 book ai didi

java - 如何将图像(应该是精确大小)写入外部存储

转载 作者:行者123 更新时间:2023-12-02 00:16:22 24 4
gpt4 key购买 nike

我正在编写一个程序,从资源/可绘制对象中读取图像并将其刷新到外部存储。我的问题是不同的设备对于同一图像消耗不同的内存,但是我需要内存消耗保持不变。例如,设备 1 显示的图像大小为 3MB,而设备 2 显示的图像大小为 4MB。下面是我的示例代码。TIA

BitmapFactory.Options options = new BitmapFactory.Options();
//setting few options
//making sure that the getByteCount will always return the same value irrespective of the device
bm = BitmapFactory.decodeResource(res, R.drawable.image, options);
bm.compress(Bitmap.CompressFormat.JPEG, 100, outStream); //where outstream is where I need to store my image in the device filesystem

最佳答案

我可以假设您的设备具有不同的屏幕尺寸和密度。因此使用不同的资源。

为了获得特定屏幕尺寸的可绘制性,您可以使用以下代码:

Drawable drawable = resources.getDrawableForDensity(id, DisplayMetrics.DENSITY_XHIGH, theme);

此外,您可以尝试减小图像的大小:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inSampleSize = 3;
BitmapFactory.decodeResource(getResources(), R.mipmap.hqimage, options);

但一般来说,您不能总是确定图像会消耗相同数量的内存,这取决于多种因素。

希望这有帮助。

关于java - 如何将图像(应该是精确大小)写入外部存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58087621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com