Android:最大允许宽度位图高度 [英] Android : Maximum allowed width & height of bitmap

查看:30
本文介绍了Android:最大允许宽度位图高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个需要将大图像解码为位图以在 ImageView 中显示的应用.

如果我只是尝试将它们直接解码为位图,则会出现以下错误"位图太大而无法上传到纹理中 (1944x2592, max=2048x2048)"

所以为了能够显示太高分辨率的图像,我使用:

Bitmap bitmap = BitmapFactory.decodeFile(path);if(bitmap.getHeight()>=2048||bitmap.getWidth()>=2048){DisplayMetrics 指标 = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(metrics);int width = metrics.widthPixels;int height = metrics.heightPixels;位图 =Bitmap.createScaledBitmap(位图,宽度,高度,真);}

这可行,但我真的不想像现在在 if 语句中那样对 2048 的最大值进行硬编码,但我无法找到如何获得设备位图的最大允许大小

有什么想法吗?

解决方案

这个限制应该来自底层的 OpenGL 实现.如果你已经在你的应用中使用了 OpenGL,你可以使用这样的东西来获得最大尺寸:

int[] maxSize = new int[1];gl.glGetIntegerv(GL10.GL_MAX_TEXTURE_SIZE, maxSize, 0);//maxSize[0] 现在包含最大尺寸(在两个维度上)

这表明我的 Galaxy Nexus 和 Galaxy S2 的最大分辨率为 2048x2048.

不幸的是,如果你还没有使用它,获得一个 OpenGL 上下文来调用它的唯一方法是创建一个(包括表面视图等),这只是查询最大大小的大量开销.

Im creating an app that needs to decode large images to bitmaps to be displayed in a ImageView.

If i just try to decode them straight to a bitmap i get the following error " Bitmap too large to be uploaded into a texture (1944x2592, max=2048x2048)"

So to be able to show images with too high resolution im using:

Bitmap bitmap = BitmapFactory.decodeFile(path);

if(bitmap.getHeight()>=2048||bitmap.getWidth()>=2048){
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;
    bitmap =Bitmap.createScaledBitmap(bitmap, width, height, true);             
}

This works but I don't really want to hardcode the maximum value of 2048 as I have in the if-statement now, but I cant find out how to get a the max allowed size of the bitmap for a device

Any ideas?

解决方案

This limit should be coming from the underlying OpenGL implementation. If you're already using OpenGL in your app, you can use something like this to get the maximum size:

int[] maxSize = new int[1];
gl.glGetIntegerv(GL10.GL_MAX_TEXTURE_SIZE, maxSize, 0);
// maxSize[0] now contains max size(in both dimensions)

This shows that my both my Galaxy Nexus and Galaxy S2 have a maximum of 2048x2048.

Unfortunately, if you're not already using it, the only way to get an OpenGL context to call this from is to create one(including the surfaceview, etc), which is a lot of overhead just to query a maximum size.

这篇关于Android:最大允许宽度位图高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆