无法在onActivityResult中加载纹理

时间:2015-03-02 11:42:43

标签: java android libgdx textures onactivityresult

这是核心项目:

public class GameClass extends Game {

    public CustomScreen customScreen;
    public CustomScreen currentScreen;

    public CustomInterface handler;

    public static int screenWidth;
    public static int screenHeight;

    public GameClass(CustomInterface handler) {
        this.handler = handler;
    }

    @Override
    public void create () {
        screenWidth = Gdx.graphics.getWidth();
        screenHeight = Gdx.graphics.getHeight();
        customScreen = new CustomScreen();
        currentScreen = customScreen;
        setScreen(currentScreen);
        handler.PickFile();
    }

    public void onPickFile(String filePath) {
        currentScreen.onPickFile(filePath);
    }
}

public class CustomObject {

    public Texture texture;

    public CustomObject() {
        this.texture = new Texture(Gdx.files.internal("badlogic.jpg"));
    }

    public void Display(SpriteBatch batcher) {
        batcher.draw(texture, 0, 0, 500, 500);
    }
}

public interface CustomInterface {
    public void PickFile();
}

public class CustomScreen implements Screen {

    Texture texture = new Texture(Gdx.files.internal("badlogic.jpg"));

    SpriteBatch batcher;
    OrthographicCamera cam;
    CustomObject obj2;

    public CustomScreen() {
        cam = new OrthographicCamera();
        cam.setToOrtho(true, GameClass.screenWidth, GameClass.screenHeight);
        batcher = new SpriteBatch();
        batcher.setProjectionMatrix(cam.combined);
    }

    public void onPickFile(String filePath) {
        obj2 = new CustomObject();
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

        batcher.begin();
        if (obj2 != null)
            obj2.Display(batcher);
        batcher.end();
    }
}

这是Android项目:

public class AndroidLauncher extends AndroidApplication implements CustomInterface {

    GameClass game;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        game = new GameClass(this);
        initialize(game, config);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        game.onPickFile("");
    }

    public void PickFile() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("image/*");
        startActivityForResult(intent, 1);
    }
}

当调用CustomScreen类中的渲染函数时,在我选择Android库中的图片后,它显示的是黑色纹理,而不是显示我期望的badlogic.jpg纹理。

这就是它显示的内容:

enter image description here

这就是它应该显示的内容:

enter image description here

(我知道Android项目中的部分代码是无用的,我基本上可以在Core项目中加载badlogic.jpg纹理,但最后要点是加载从Android库中挑选的纹理,但是如你所见,我甚至无法在资产文件夹中加载纹理)

我一直试图解决这个问题好几个月了,我仍然不知道怎么做,请帮助我!

0 个答案:

没有答案