简体   繁体   English

Libgdx从Gdx.files.external()加载纹理显示为黑色矩形

[英]Libgdx load texture from Gdx.files.external() showing as black rectangles

I have added both the external read and write permissions and requested permission for them from the user. 我已经添加了外部读写权限,并从用户那里请求了它们的权限。 I have used the libgdx file system to generate a list of filehandles of pictures I would like to load. 我已经使用libgdx文件系统生成了我要加载的图片的文件句柄列表。 I load them like this: 我这样加载它们:

image = new Texture(fh); //fh is the filehandle

When I render any of the images I tried to load it just renders a black rectangle. 当我渲染任何图像时,我尝试加载的只是渲染一个黑色矩形。 I render them just using a spritebatch with the .draw method like I normally would. 我像通常那样使用带有.draw方法的spritebatch渲染它们。 Any idea why they are rendering as black rectangles? 知道为什么它们呈现为黑色矩形吗? Thanks in advance/ 提前致谢/

My image module code: 我的图像模块代码:

package com.ggi.uparty.ui;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.ggi.uparty.screens.ImagePicker;

public class ImagePickerModule {

public ImagePicker p;

public FileHandle fh;

public Texture image = null;

public TextureRegion load,preview;


public float theta = 0;

public Rectangle bounds = new Rectangle();

public ImagePickerModule(ImagePicker p, final FileHandle fh){
    theta = 0;
    bounds.width=.85f*Gdx.graphics.getWidth()/6f;
    bounds.height=bounds.width;

    load = new TextureRegion(p.u.assets.get("UI/Load.png", Texture.class));

    Thread t = new Thread(new Runnable(){

        @Override
        public void run() {
            try{
            //image = new Texture(fh);

            image = new Texture(fh);
            System.out.println(fh.path());
            float sqSize = image.getWidth()<image.getHeight()?image.getWidth():image.getHeight();
            preview = new TextureRegion(image,bounds.x+bounds.width/2-sqSize/2,bounds.y+bounds.height/2-sqSize/2,sqSize,sqSize);
            }catch(Exception e){
                e.printStackTrace();
            }

        }

    });
    t.start();
}

public void draw(SpriteBatch pic,float fade){
    theta++;
    pic.setColor(1, 1, 1, fade);
    if(image == null){
        pic.draw(load, bounds.x+bounds.width / 2 - bounds.height / 4, bounds.y + bounds.height / 4+theta, bounds.height / 4,
                bounds.height / 4, bounds.height / 2, bounds.height / 2, 1, 1, -theta);
    }
    else{
        pic.draw(image,bounds.x,bounds.y+theta,bounds.width,bounds.height);
    }
}

}

You are trying to load a texture on a non-ui thread. 您正在尝试在非UI线程上加载纹理。 If you want async loading of textures use the assetmanager. 如果要异步加载纹理,请使用assetmanager。

To test this, try changing the Thread.start to thread.run (run immediately in the thread you are on now, so disabling the thread) the image should load fine. 要对此进行测试,请尝试将Thread.start更改为thread.run(立即在您现在所在的线程中运行,因此禁用该线程),图像应该可以正常加载。

then to get async loading implement an assetmanager: 然后要获取异步加载,请实施资产管理器:

https://github.com/libgdx/libgdx/wiki/Managing-your-assets https://github.com/libgdx/libgdx/wiki/Managing-your-assets

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM