簡體   English   中英

如何在不設置OpenGL上下文的情況下可靠地確定ImageView可顯示的最大圖像大小?

[英]How to reliably determine the maximum image size displayable by ImageView without setting up an OpenGL context?

我的應用程序顯示從相機拍攝的照片。 在某些設備上,由於以下錯誤消息,導致圖片未顯示在ImageView上: Bitmap too large to be uploaded into a texture (1624x2251, max=2048x2048)要解決此問題,該應用程序只是將圖像按比例縮小到支持的最大紋理大小。 代碼如下:

int[] maxTextureSize = new int[1];
GLES10.glGetIntegerv(GLES10.GL_MAX_TEXTURE_SIZE, maxTextureSize, 0);
if (maxTextureSize[0] < 2048) {
    maxTextureSize[0] = 2048;
}

if (bitmap.getWidth() > maxTextureSize[0] || bitmap.getHeight() > maxTextureSize[0]) {
    float factor = Math.min((float)maxTextureSize[0] / (float)bitmap.getWidth(), (float)maxTextureSize[0] / (float)bitmap.getHeight());
    int newWidth = (int) (bitmap.getWidth() * factor);
    int newHeight = (int) (bitmap.getHeight() * factor);

    bitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, false);
}

現在這在大多數情況下都適用,但有時(隨機)除外,這時GLES10.glGetIntegerv(GLES10.GL_MAX_TEXTURE_SIZE)導致以下錯誤消息而不返回紋理大小:

call to OpenGL ES API with no current context (logged once per thread)

現在的問題是,應用程序如何可靠地確定ImageView的最大可顯示位圖大小? 最好不設置OpenGL上下文。 有沒有其他選擇?

看一下有效顯示位圖 這是一個偉大的深入官方指導解決您所遇到的確切的問題。

這個關於堆棧溢出的答案中 ,您可以使用:

MemoryInfo mi = new MemoryInfo();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.getMemoryInfo(mi);
long availableMegs = mi.availMem / 1048576L;

//Percentage can be calculated for API 16+
long percentAvail = mi.availMem / mi.totalMem;

獲取您的應用可用的內存。 使用這個值和位圖的大小,您可以精確地確定圖像的大小。

該應用程序僅將圖像縮小到最大支持的紋理大小

我必須說,您可能正在嘗試渲染大於顯示的圖像。 您是否在渲染位圖之前考慮設備的分辨率? 並不是很多設備都是2048像素

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM