来自资源的图像已正确缩放,但来自 SQlite 的图像 blob 未正确缩放 - Android

发布于 2024-11-29 07:37:51 字数 747 浏览 2 评论 0原文

我遇到了一个奇怪的问题。我的 Android 项目中有多个图像,我将它们作为 .png 文件存储在 res\drawable 下。我能够在运行时轻松提取图像并将其转换为位图,如下所示:

Drawable d = getResources().getDrawable(R.drawable.imageId);
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();

这非常有效,并且无论设备的屏幕密度如何,图像都会正确缩放。我的所有图像均为 200 像素 x 200 像素,图像布局配置为 200 倾角 x 200 倾角。

现在,由于可扩展性问题,我已将所有图像作为 blob 存储在 SQlite 数据库中,并且在运行时提取它们并转换为如下所示的位图:

byte[] bb = cursor.getBlob(columnIndex);
Bitmap bitmap = BitmapFactory.decodeByteArray(bb, 0, bb.length);

如果屏幕密度为标准 160 度倾斜,则图像显示正常。但如果密度更低或更高,图像不会缩放,并且在 160 次倾角时仍保持 200 像素 x 200 像素。因此,基本上,在较小的屏幕(120 个倾角)上,图像占用的空间比应有的空间多,而在较大的屏幕(240 个倾角)上,图像占用的空间比应有的空间少。

还有其他人遇到过这个奇怪的问题吗?任何解释、解决方法、解决方案将不胜感激。

提前非常感谢!

I am running into a strange issue. I have multiple images in my Android project which I stored as .png files under res\drawable. I was able to easily extract the images at runtime and convert them to a bitmap like this:

Drawable d = getResources().getDrawable(R.drawable.imageId);
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();

This works great and the image gets scaled correctly no matter what screen density the device has. All my images are 200 pixels by 200 pixels and my image layout is configured as 200 dip x 200 dip.

Now, I have stored all images as blobs in an SQlite database due to scalability issues and I am extracting them at runtime and converting to a bitmap like this:

byte[] bb = cursor.getBlob(columnIndex);
Bitmap bitmap = BitmapFactory.decodeByteArray(bb, 0, bb.length);

The image displays fine if the screen density is standard 160 dip. But if the density is any less or more, the image doesn't scale and remains 200 pixels x 200 pixels for 160 dip. So basically, on a smaller screen (120 dip), the image takes more space than it should and on a larger screen (240 dip), it takes less space than it should.

Has anyone else run into this bizarre issue? Any explanation, workaround, solution will be really appreciated.

Thanks much in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

時窥 2024-12-06 07:37:51

好吧,我终于通过使用 createScaledBitmap() 让它工作了。

从 blob 创建位图后,我计算缩放因子,然后计算新的宽度和高度。然后我在 createScaledBitmap() 函数中使用它们。这是有效的代码:

public static Bitmap getImageFromBlob(byte[] mBlob) 
{
    byte[] bb = mBlob;
    Bitmap b = BitmapFactory.decodeByteArray(bb, 0, bb.length); 
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int newWidth = b.getWidth()*metrics.densityDpi)/DisplayMetrics.DENSITY_DEFAULT;
    int newHeight = b.getHeight()*metrics.densityDpi)/DisplayMetrics.DENSITY_DEFAULT;
    return Bitmap.createScaledBitmap(b, newWidth, newHeight,  true);
}

此链接提供了线索:
缩放位图方法之间的差异

Okay, I finally got it to work by using createScaledBitmap().

After creating a bitmap from the blob, I calculate the scaling factor and then calculate the new width and height. I then use those in the createScaledBitmap() function. Here's the code that works:

public static Bitmap getImageFromBlob(byte[] mBlob) 
{
    byte[] bb = mBlob;
    Bitmap b = BitmapFactory.decodeByteArray(bb, 0, bb.length); 
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int newWidth = b.getWidth()*metrics.densityDpi)/DisplayMetrics.DENSITY_DEFAULT;
    int newHeight = b.getHeight()*metrics.densityDpi)/DisplayMetrics.DENSITY_DEFAULT;
    return Bitmap.createScaledBitmap(b, newWidth, newHeight,  true);
}

This link provided the clue:
difference between methods to scale a bitmap

苍风燃霜 2024-12-06 07:37:51

使用decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Options opts) 并在opts中将inDesity设置为160。

Use decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Options opts) and in opts set inDesity to let say 160.

少跟Wǒ拽 2024-12-06 07:37:51

为我工作,请参阅这个。但我不得不说,我认为 SQLite 做得对,而且 android 绘图的大小不正确。

Worked for me, see this. But I have to say, I think SQLite is doing it right, and the android drawable is incorrectly sized.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文