【问题标题】:Out Of Memory on Assets.Reload after Purchasing Item资产内存不足。购买项目后重新加载
【发布时间】:2015-06-04 11:37:30
【问题描述】:

我开发了一个使用 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 发生在真实设备上(在三星 Galaxy Young 和三星 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但是,它说分配缩放位图失败

【问题讨论】:

标签: android bitmap out-of-memory


【解决方案1】:

你应该重构这个

位图位图 = 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

【讨论】:

  • 我编辑了代码并在我的问题中更新了它。请看一下。
猜你喜欢
  • 2016-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多