M.Mohsin
M.Mohsin

Reputation: 314

Loading image as Sprite from SD card in AndEngine GLES2

I have searched all the forums but i am unable to get any working code. I want to load an image as sprite from SD Card in AndEngine GLES2.Can any one provide me a code?
Any Help will be Appreciated. Here is what i have tried.

File imageFile = new File(pFilePath);
BitmapTextureAtlas texture = new BitmapTextureAtlas(myCxt.getTextureManager(),540 , 960 , TextureOptions.DEFAULT);
FileBitmapTextureAtlasSource fileTextureSource = new FileBitmapTextureAtlasSource(imageFile);
TextureRegion textureRegion = TextureRegionFactory.createFromSource(texture,fileTextureSource ,0,0, true);

This code is working for andEngine1 but in GLES2, line given below is generating compile time error
"Construtor FileBitmapTextureAtlasSource(file) is undefined"
FileBitmapTextureAtlasSource fileTextureSource = new FileBitmapTextureAtlasSource(imageFile);

How can i load an image as sprite from SD Card.

Upvotes: 2

Views: 1327

Answers (3)

M.Mohsin
M.Mohsin

Reputation: 314

I have found the solution of the problem.
FileBitmapTextureAtlasSource is changed in AndEngine2.
Here is the code that worked for me

File imageFile = new File(pFilePath);
BitmapTextureAtlas texture = new BitmapTextureAtlas(getTextureManager(),displayMetrics.widthPixels , displayMetrics.heightPixels , TextureOptions.BILINEAR_PREMULTIPLYALPHA);
FileBitmapTextureAtlasSource fileTextureSource = FileBitmapTextureAtlasSource.create(imageFile);
Engine.getTextureManager().loadTexture(texture);
TextureRegion textureRegion = TextureRegionFactory.createFromSource(texture,fileTextureSource ,0,0, false);
screenShot = new Sprite(130, 230, textureRegion, myCxt.getVertexBufferObjectManager());

Upvotes: 3

正宗白布鞋
正宗白布鞋

Reputation: 929

Just direct copy from my old project (it is inside activity class), it load image from internal storage, you can change it to createFromExternalStorage(). IIRC, createFromExternalStorage() and createFromInternalStorage() have the same parameters, but different locations. The image file is located at '/data/data/your_package/files/' (if internal) or '/sdcard/Android/your_package/files/' (if external).

BitmapTextureAtlas texture = new BitmapTextureAtlas(this.getTextureManager(), 1024, 1024);
mBeanRegion = BitmapTextureAtlasTextureRegionFactory.createFromSource(texture, FileBitmapTextureAtlasSource.createFromInternalStorage(this, "bean.png", 0, 0), 0, 0);
texture.load();

Upvotes: 1

Developer
Developer

Reputation: 6350

Try this

File imageFile = new File("sdcard/image");
BitmapTextureAtlas texture = new BitmapTextureAtlas(width , height , TextureOptions.DEFAULT);
FileBitmapTextureAtlasSource fileTextureSource = new FileBitmapTextureAtlasSource(imageFile);
TextureRegion textureRegion = TextureRegionFactory.createFromSource(texture,fileTextureSource ,0,0, true);

Upvotes: 0

Related Questions