购买物品后 Assets.Reload 内存不足
Out Of Memory on Assets.Reload after Purchasing Item
我开发了一个使用 2 个纹理(一个是 2048x2048,另一个是 1024x2048)的游戏,此外我使用 7 个背景图片作为关卡图像(每个 640x960)。
当付款成功弹出时(应用程序计费)我得到了内存不足(OOM),我跟踪日志并发现错误是在 运行 onSurfaceCreated 时出现的,其中代码 assets.reload 在重新加载我所有的纹理和背景图片的地方执行。
不知道该怎么做,因为如果我不重新加载,我将无法获得正确的图像。
仅供参考: assets.reload 也会在 pause/resume 执行时调用(当我在播放时按下主页按钮时),但它永远不会成为问题。
public class Texture {
GLGraphics glGraphics;
FileIO fileIO;
String fileName;
int textureId;
int minFilter;
int magFilter;
int width;
int height;
public Texture(GLGame glGame, String fileName) {
this.glGraphics = glGame.getGLGraphics();
this.fileIO = glGame.getFileIO();
this.fileName = fileName;
load();
}
private void load() {
GL10 gl = glGraphics.getGL();
int[] textureIds = new int[1];
gl.glGenTextures(1, textureIds, 0);
textureId = textureIds[0];
InputStream in = null;
try {
in = fileIO.readAsset(fileName);
Bitmap bitmap = BitmapFactory.decodeStream(in);
width = bitmap.getWidth();
height = bitmap.getHeight();
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
setFilters(GL10.GL_NEAREST, GL10.GL_NEAREST);
gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
} catch(IOException e) {
throw new RuntimeException("Couldn't load texture '" + fileName + "'", e);
} finally {
if(in != null)
try { in.close(); } catch (IOException e) { }
}
}
public void reload() {
load();
bind();
setFilters(minFilter, magFilter);
glGraphics.getGL().glBindTexture(GL10.GL_TEXTURE_2D, 0);
}
public void setFilters(int minFilter, int magFilter) {
this.minFilter = minFilter;
this.magFilter = magFilter;
GL10 gl = glGraphics.getGL();
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, minFilter);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, magFilter);
}
public void bind() {
GL10 gl = glGraphics.getGL();
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
}
public void dispose() {
GL10 gl = glGraphics.getGL();
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
int[] textureIds = { textureId };
gl.glDeleteTextures(1, textureIds, 0);
}
}
正如你在代码中看到的(这是从 mario zachner 开始的 android 游戏框架),assets.reload 来自这个 texture.reload。
OOM发生在真实设备中(在samsung galaxy young和samsung galaxy note II上测试过)
附加问题:购买过程是否强制应用进入暂停模式?
更新/对答案的回应:
根据您的建议,我修改了代码:
in = fileIO.readAsset(fileName);
BitmapFactory.Options bfOptions=new BitmapFactory.Options();
bfOptions.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeStream(in,null,bfOptions);
int imageHeight = bfOptions.outHeight;
int imageWidth = bfOptions.outWidth;
bfOptions.inJustDecodeBounds = false;
bfOptions.inSampleSize = calculateInSampleSize(bfOptions,imageWidth,imageHeight);
bfOptions.inDither=false; //Disable Dithering mode
bfOptions.inPurgeable=true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared
bfOptions.inInputShareable=true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
bfOptions.inTempStorage=new byte[32 * 1024];
bitmap = BitmapFactory.decodeStream(in,null,bfOptions);
width = bitmap.getWidth();
height = bitmap.getHeight();
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
setFilters(GL10.GL_NEAREST, GL10.GL_NEAREST);
gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
但是当它 运行 我得到了其他错误:仍然是 OOM 但是,它说 分配缩放位图失败
你应该重新审视这个
位图位图=BitmapFactory.decodeStream(in);
您可以使用
public static Bitmap decodeSampledBitmapFromFile(String photoPath) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
//Here add more optiouns to optimize
// options.inSampleSize = ...
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
return bitmap;
}
看这里Android image resize
我开发了一个使用 2 个纹理(一个是 2048x2048,另一个是 1024x2048)的游戏,此外我使用 7 个背景图片作为关卡图像(每个 640x960)。
当付款成功弹出时(应用程序计费)我得到了内存不足(OOM),我跟踪日志并发现错误是在 运行 onSurfaceCreated 时出现的,其中代码 assets.reload 在重新加载我所有的纹理和背景图片的地方执行。
不知道该怎么做,因为如果我不重新加载,我将无法获得正确的图像。
仅供参考: assets.reload 也会在 pause/resume 执行时调用(当我在播放时按下主页按钮时),但它永远不会成为问题。
public class Texture {
GLGraphics glGraphics;
FileIO fileIO;
String fileName;
int textureId;
int minFilter;
int magFilter;
int width;
int height;
public Texture(GLGame glGame, String fileName) {
this.glGraphics = glGame.getGLGraphics();
this.fileIO = glGame.getFileIO();
this.fileName = fileName;
load();
}
private void load() {
GL10 gl = glGraphics.getGL();
int[] textureIds = new int[1];
gl.glGenTextures(1, textureIds, 0);
textureId = textureIds[0];
InputStream in = null;
try {
in = fileIO.readAsset(fileName);
Bitmap bitmap = BitmapFactory.decodeStream(in);
width = bitmap.getWidth();
height = bitmap.getHeight();
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
setFilters(GL10.GL_NEAREST, GL10.GL_NEAREST);
gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
} catch(IOException e) {
throw new RuntimeException("Couldn't load texture '" + fileName + "'", e);
} finally {
if(in != null)
try { in.close(); } catch (IOException e) { }
}
}
public void reload() {
load();
bind();
setFilters(minFilter, magFilter);
glGraphics.getGL().glBindTexture(GL10.GL_TEXTURE_2D, 0);
}
public void setFilters(int minFilter, int magFilter) {
this.minFilter = minFilter;
this.magFilter = magFilter;
GL10 gl = glGraphics.getGL();
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, minFilter);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, magFilter);
}
public void bind() {
GL10 gl = glGraphics.getGL();
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
}
public void dispose() {
GL10 gl = glGraphics.getGL();
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
int[] textureIds = { textureId };
gl.glDeleteTextures(1, textureIds, 0);
}
}
正如你在代码中看到的(这是从 mario zachner 开始的 android 游戏框架),assets.reload 来自这个 texture.reload。
OOM发生在真实设备中(在samsung galaxy young和samsung galaxy note II上测试过)
附加问题:购买过程是否强制应用进入暂停模式?
更新/对答案的回应: 根据您的建议,我修改了代码:
in = fileIO.readAsset(fileName);
BitmapFactory.Options bfOptions=new BitmapFactory.Options();
bfOptions.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeStream(in,null,bfOptions);
int imageHeight = bfOptions.outHeight;
int imageWidth = bfOptions.outWidth;
bfOptions.inJustDecodeBounds = false;
bfOptions.inSampleSize = calculateInSampleSize(bfOptions,imageWidth,imageHeight);
bfOptions.inDither=false; //Disable Dithering mode
bfOptions.inPurgeable=true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared
bfOptions.inInputShareable=true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
bfOptions.inTempStorage=new byte[32 * 1024];
bitmap = BitmapFactory.decodeStream(in,null,bfOptions);
width = bitmap.getWidth();
height = bitmap.getHeight();
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
setFilters(GL10.GL_NEAREST, GL10.GL_NEAREST);
gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
但是当它 运行 我得到了其他错误:仍然是 OOM 但是,它说 分配缩放位图失败
你应该重新审视这个
位图位图=BitmapFactory.decodeStream(in);
您可以使用
public static Bitmap decodeSampledBitmapFromFile(String photoPath) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
//Here add more optiouns to optimize
// options.inSampleSize = ...
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
return bitmap;
}
看这里Android image resize