Al Lelopath
Al Lelopath

Reputation: 6778

Load TextureRegion from drawable resource

I have this code working:

menu2TextureAtlas = new BitmapTextureAtlas(this.activity.getTextureManager(),256, 128);
menu2TextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menu2TextureAtlas, this.activity, "menu2.png", 0, 0);
menu2TextureAtlas.load();

but I've now moved the image menu2.png to res/drawable-* (Where * is hdpi, mdpi, xhdpi, xxhdpi). So now I need to load the image from there. How do I do this?

UPDATE

Answer, not sure if its the best:

Resources resources = activity.getResources();
Drawable drawable = resources.getDrawable(R.drawable.menu1);
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
menu1TextureAtlas = new BitmapTextureAtlas(this.activity.getTextureManager(), width, height);
menu1TextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromResource(menu1TextureAtlas, this.activity, R.drawable.menu1, 0, 0);
menu1TextureAtlas.load();

Upvotes: 2

Views: 547

Answers (2)

Al Lelopath
Al Lelopath

Reputation: 6778

The answer is at end of original post

Upvotes: 0

Plastic Sturgeon
Plastic Sturgeon

Reputation: 12527

Here is code I have used to load a png from internal storage instead of a drawable. Hopefully this will fit your needs. In fact, in my own case, I start by saving a drawable to the internal storage so that I can overwrite the file in storage. Not sure if this will fit you case, but here is the code. If you need more, just add a comment.

    public void loadImageFromInternalStorage(){
        Log.d(TAG, "loadImageFromInternalStorage()");
        try {
                File imageFile = this.getFileStreamPath(userCreatedFileName);
                FileInputStream fi = new FileInputStream(imageFile); // Throws an error if file not exits.
                FileBitmapTextureAtlasSource fileTextureSource = FileBitmapTextureAtlasSource.create(imageFile);
                this.grabTexture.clearTextureAtlasSources();
                Log.d(TAG, "fileTextureSource" + fileTextureSource.toString());
                BitmapTextureAtlasTextureRegionFactory.createFromSource(grabTexture, fileTextureSource, 0, 0);
                Log.d(TAG, "createdFromResource");

        } catch (FileNotFoundException ex) {
                Log.e("File Not found in internal storage", ex.getMessage());

        }
    }

Upvotes: 1

Related Questions