在购买物品

时间:2015-06-04 11:37:30

标签: android bitmap out-of-memory

我开发了一个使用2个纹理的游戏(一个是2048x2048,另一个是1024x2048),我使用7张背景图片作为关卡图像(每个640x960)。

当支付成功弹出(inapp计费)时,我得到了内存不足(OOM),我跟踪日志并得到错误是在运行onSurfaceCreated时出现的,其中代码assets.reload在我的所有纹理和背景图片重新加载。

不知道该怎么办,因为如果我不重新加载我将无法获得正确的图像。

仅供参考:在执行暂停/恢复时(当我在播放时按下主页按钮时)也会调用assets.reload,但它永远不会成为问题。

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发生在真实设备上(在三星星系年轻和三星星系注二号测试)

其他问题:购买流程是否强制应用进入暂停模式?

更新/回应答案: 根据你的建议,我修改了代码:

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但是,它说分配比例缩放位图失败

1 个答案:

答案 0 :(得分:0)

你应该反思这个

Bitmap bitmap = 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