加载并显示图库照片后,libGDX黑屏

时间:2016-12-14 23:22:25

标签: android image libgdx

有没有人有一个很好的例子来加载照片库中的图像并在libGDX中呈现它?

我正在尝试以下方法,但它不会渲染加载的纹理(它只是黑色)。通过资产加载的其他纹理渲染得很好。

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    this.activity.startActivityForResult(photoPickerIntent, SELECT_PHOTO);

然后,

protected Texture getLibGdxTextureFromInputStream(InputStream is) {
    Bitmap bitmap = BitmapFactory.decodeStream(is);

    Texture texture = new Texture(bitmap.getWidth(), bitmap.getHeight(), Format.RGBA8888);
    Gdx.gl20.glBindTexture(GL20.GL_TEXTURE_2D, texture.getTextureObjectHandle());
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
    Gdx.gl20.glBindTexture(GL20.GL_TEXTURE_2D, 0);
    bitmap.recycle();

    return texture;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode) { 
        case SELECT_PHOTO:
            if (resultCode == Activity.RESULT_OK) {  
                try {
                    Uri selectedImageUri = data.getData();
                    InputStream imageStream = getContentResolver().openInputStream(selectedImageUri);
                    Texture texture = getLibGdxTextureFromInputStream(imageStream);

                    Screen screen = game.getScreen();
                    if (screen instanceof MyGameScreen) {
                        ((MyGameScreen)screen).galleryImageSelected(texture);
                    }
                } catch (FileNotFoundException e) {
                    Screen screen = game.getScreen();
                    if (screen instanceof MyGameScreen) {
                        ((MyGameScreen)screen).galleryImageSelected(null);
                    }
                }
            }
            break;
    }
}

在我的游戏画面中:

public void galleryImageSelected(final Texture texture) {
    if (texture != null) {
        // set the background texture to the image pickers active texture
        Gdx.app.postRunnable(new Runnable() {
            @Override
            public void run() {
                galleryImageTexture = texture;
            }
        });
    } else {
        // display a message e.g. "image format not supported"
        // TODO:
    }
}

然后我尝试在我的渲染循环中渲染:

batch.draw(galleryImageTexture, 0, 0);

但它没有渲染。

更新

这只是更奇怪......我已经将它缩小到了从onActivityResult方法设置galleryImageTexture的问题。

即使我返回硬编码的纹理,例如新的纹理(" test.png")从那里它仍然没有渲染,而如果我设置它,例如在活动的负载,然后它呈现良好......

0 个答案:

没有答案