在图片预览时,偶现图片无法现实,在查看程序的时候发现Bitmap是实际存在的,但是在ImageView中缺绘制不出来,这个问题困然了我很久,查看代码也查不出原因,再加上是偶现的,查原因时费了不少时间。
先来看看异常:
10-14 09:53:34.902 9003-10023/? W/OpenGLRenderer﹕ Bitmap too large to be uploaded into a texture (1024x9297, max=8192x8192)
10-14 09:53:34.902 9003-10023/? W/OpenGLRenderer﹕ Shape too large to be rendered into a texture (940x8510, max=8192x8192)
10-14 09:53:34.917 9003-10023/? W/OpenGLRenderer﹕ Bitmap too large to be uploaded into a texture (1024x9297, max=8192x8192)
10-14 09:53:34.919 9003-10023/? W/OpenGLRenderer﹕ Bitmap too large to be uploaded into a texture (1024x9297, max=8192x8192)
10-14 09:53:34.919 9003-10023/? W/OpenGLRenderer﹕ Bitmap too large to be uploaded into a texture (1024x9297, max=8192x8192)
其实是因为图片过长的原因,图片可以绘制的最大高度为8192,而我的图片高度为9297,在Canvas绘制时,OpenGL无法绘制出来,所以导致在预览长图时无法显示出来。
修改方式:
1.在异步加载图片时,判断图片的宽高,如果高度超出了底层OpenGL绘制的高度,那就把图片缩小。代码如下:
public void loadImage(final MediaItem mediaItem) {
if (mBitmap == null && !mDestoryDecodeThread && !mDestory) {
mHardWareDecodeFuture = ThreadPool.getInstance().submit(new ThreadPool.Job<Bitmap>() {
@Override
public Bitmap run(ThreadPool.JobContext jc) {
boolean rotated = ((mediaItem.getRotation() / 90) & 1) == 1;
int photoWidth = rotated ? mediaItem.getHeight() : mediaItem.getWidth();
int photoHeight = rotated ? mediaItem.getWidth() : mediaItem.getHeight();
final BitmapFactory.Options options = new BitmapFactory.Options();
jc.setCancelListener(new ThreadPool.CancelListener() {
@Override
public void onCancel() {
options.requestCancelDecode();
}
});
InputStream is = null;
try {
Bitmap bitmap = null;
Uri uri = mediaItem.getContentUri();
int sampleSize = 1;
if (photoHeight > mHeight || photoWidth > mWidth) {
final int heightRatio = (int) Math.floor((float) photoHeight / (float) mHeight);
final int widthRatio = (int) Math.floor((float) photoWidth / (float) mWidth);
sampleSize = heightRatio > widthRatio ? heightRatio : widthRatio;
}
options.inSampleSize = sampleSize;
if (bitmap != null) {
options.inBitmap = bitmap;
} if (uri.toString().startsWith("file://")) {
bitmap = BitmapFactory.decodeFile(uri.toString().substring("file://".length(), uri.toString().length()), options);
} else {
is = getContext().getContentResolver().openInputStream(uri);
bitmap = BitmapFactory.decodeStream(is, null, options);
} /*重点缩小图片大小的代码*/
int maxHeight = EglUtil.getMaxTextureSize(getContext());
if (bitmap.getHeight() > maxHeight) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
while (h > maxHeight) {
w = w / 2;
h = h / 2;
}
Bitmap b = Bitmap.createScaledBitmap(bitmap, w, h, true);
bitmap.recycle();
bitmap = b;
}
/**/ if (jc.isCancelled() || mDestory || mDestoryDecodeThread) {
WeakReference<Bitmap> ref = new WeakReference<Bitmap>(bitmap);
return null;
}
return bitmap;
} catch (Exception ex) {
} finally {
MediaItem.closeSilently(is);
}
return null;
}
}, new FutureListener<Bitmap>() {
@Override
public void onFutureDone(final Future<Bitmap> future) {
if (future != null && future.get() != null) {
post(new Runnable() {
@Override
public void run() {
mBitmap = future.get();
if (!mStopDrawBigBmp && !mDestoryDecodeThread && !mDestory) {
mScreenNailLoaded = true;
initBitmapRect();
invalidate();
} else {
if (mBitmap != null && !mBitmap.isRecycled()) {
mBitmap.recycle();
}
} }
});
}
}
});
}
}
2.判断图片是否超过绘制的最高高度。没一台手机,可绘制的最大高度是有可能不一样的,在Android5.0以上的版本中可以获取绘制的最大高度值,代码如下:
public static int getMaxTextureSize(Context context){
if(maxTextureSize != -1){
return maxTextureSize;
}
maxTextureSize = 0;
int[] maxSize = new int[1];
try {
ConfigurationInfo configurationInfo = ((ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE)).getDeviceConfigurationInfo();
int glesVersion = configurationInfo.reqGlEsVersion;
if(Build.VERSION.SDK_INT >= 21) {
//configureEGLContext
EGLDisplay mEGLDisplay = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY);
if (mEGLDisplay == EGL14.EGL_NO_DISPLAY) {
throw new IllegalStateException("No EGL14 display");
}
int[] version = new int[2];
if (!EGL14.eglInitialize(mEGLDisplay, version, /*offset*/ 0, version, /*offset*/ 1)) {
throw new IllegalStateException("Cannot initialize EGL14");
}
int[] attribList = {
EGL14.EGL_RED_SIZE, 8,
EGL14.EGL_GREEN_SIZE, 8,
EGL14.EGL_BLUE_SIZE, 8,
EGL14.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT,
//EGL_RECORDABLE_ANDROID, 1,
EGL14.EGL_SURFACE_TYPE, EGL14.EGL_PBUFFER_BIT | EGL14.EGL_WINDOW_BIT,
EGL14.EGL_NONE
};
EGLConfig[] configs = new EGLConfig[1];
int[] numConfigs = new int[1];
EGL14.eglChooseConfig(mEGLDisplay, attribList, /*offset*/ 0, configs, /*offset*/ 0,
configs.length, numConfigs, /*offset*/ 0);
if (EGL14.eglGetError() != EGL14.EGL_SUCCESS) {
throw new IllegalStateException("eglCreateContext RGB888+recordable ES2" + ": EGL error: 0x" + Integer.toHexString(EGL14.eglGetError()));
}
int[] attrib_list = {
EGL14.EGL_CONTEXT_CLIENT_VERSION, 2,
EGL14.EGL_NONE
};
EGLContext mEGLContext = EGL14.eglCreateContext(mEGLDisplay, configs[0], EGL14.EGL_NO_CONTEXT,
attrib_list, /*offset*/ 0);
if (EGL14.eglGetError() != EGL14.EGL_SUCCESS) {
throw new IllegalStateException("eglCreateContext" + ": EGL error: 0x" + Integer.toHexString(EGL14.eglGetError()));
}
if (mEGLContext == EGL14.EGL_NO_CONTEXT) {
throw new IllegalStateException("No EGLContext could be made");
}
int[] surfaceAttribs = {
EGL14.EGL_WIDTH, 64,
EGL14.EGL_HEIGHT, 64,
EGL14.EGL_NONE
};
EGLSurface surface = EGL14.eglCreatePbufferSurface(mEGLDisplay, configs[0], surfaceAttribs, 0);
EGL14.eglMakeCurrent(mEGLDisplay, surface, surface, mEGLContext);
//getMaxTextureSize
if(glesVersion >= 0x20000) {
GLES20.glGetIntegerv(GLES20.GL_MAX_TEXTURE_SIZE, maxSize, 0);
}else if(glesVersion >= 0x10000) {
GLES10.glGetIntegerv(GLES10.GL_MAX_TEXTURE_SIZE, maxSize, 0);
}
//releaseEGLContext
EGL14.eglMakeCurrent(mEGLDisplay, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_CONTEXT);
EGL14.eglDestroyContext(mEGLDisplay, mEGLContext);
EGL14.eglReleaseThread();
EGL14.eglTerminate(mEGLDisplay);
}else {
if(glesVersion >= 0x20000) {
GLES20.glGetIntegerv(GLES20.GL_MAX_TEXTURE_SIZE, maxSize, 0);
}else if(glesVersion >= 0x10000) {
GLES10.glGetIntegerv(GLES10.GL_MAX_TEXTURE_SIZE, maxSize, 0);
}
}
} catch (IllegalStateException e) {
e.printStackTrace();
}
maxTextureSize = maxSize[0] > 0 ? maxSize[0] : DEFAULT_MAX_BITMAP_DIMENSION;
return maxTextureSize;
}
注意:以上获取最大高度值是在android5.0以上的版本才行。android5.0以下的版本使用:
private static final int DEFAULT_MAX_BITMAP_DIMENSION = 8196;
这样就能保证不管图片有多长,都能显示出来了。