image - Unable to make a sprite from a URL in UI Thread in Andengine Android -
please see below code.
activity ac= (activity)cxt; ac.runonuithread(new runnable() { @override public void run() { load(); } }); this constructor calling function load sprite url. function below
private void load() { if(isinterneton()){ try { itexture mtexture = new bitmaptexture(mengine.gettexturemanager(), new iinputstreamopener() { @override public inputstream open() throws ioexception { url url = new url("http://tenlogix.com/cupcakemania/ads/burgermaker.png"); httpurlconnection connection = (httpurlconnection) url.openconnection(); connection.setdoinput(true); connection.connect(); inputstream input = connection.getinputstream(); bufferedinputstream in = new bufferedinputstream(input); return in; } }); mtexture.load(); myimagefromweb = textureregionfactory.extractfromtexture(mtexture); } catch (ioexception e) { log.d("tenlogixads"," "+e); } adsprite = new sprite(0, 0, myimagefromweb, mengine.getvertexbufferobjectmanager()); } else{ log.d("tenlogixads"," no internet connection detected.. "); adsprite = null; } } the problem image loaded if call load function in ui thread not loaded when call in ui thread.
calling in ui thread important because want task background causing issues game running.
i have tried asynctask problem described ui thread same async task.
can 1 me problem?
i want load image web, , want loading in background causing delay or issues game being played.
can use async task debug value mtexture not null.
public void loadimagefromserver() { final iasynccallback callback = new iasynccallback() { @override public void worktodo() { log.e("baseactivity", "onwhat working"); if (isinterneton()) { try { itexture mtexture = new bitmaptexture( mengine.gettexturemanager(), new iinputstreamopener() { @override public inputstream open() throws ioexception { url url = new url( "http://tenlogix.com/cupcakemania/ads/burgermaker.png"); httpurlconnection connection = (httpurlconnection) url .openconnection(); connection.setdoinput(true); connection.connect(); inputstream input = connection .getinputstream(); bufferedinputstream in = new bufferedinputstream( input); return in; } }); if (mtexture != null) { mtexture.load(); myimagefromweb = textureregionfactory .extractfromtexture(mtexture); } } catch (ioexception e) { log.d("tenlogixads", " " + e); } } else { log.d("tenlogixads", " no internet connection detected.. "); adsprite = null; } } @override public void oncomplete() { // can create after initializing myimagefromweb if (myimagefromweb != null) { adsprite = new sprite(0, 0, myimagefromweb, mengine.getvertexbufferobjectmanager()); } } }; abaseactivity.runonuithread(new runnable() { @override public void run() { new asynctaskloader().execute(callback); } }); } iasynccallback interface. looks like:
public interface iasynccallback { // =========================================================== // methods // =========================================================== public abstract void worktodo(); public abstract void oncomplete(); } and asynctaskloader class like:
import android.os.asynctask; public class asynctaskloader extends asynctask<iasynccallback, integer, boolean> { // =========================================================== // fields // =========================================================== iasynccallback[] _params; // =========================================================== // inherited methods // =========================================================== @override protected boolean doinbackground(iasynccallback... params) { this._params = params; int count = params.length; (int = 0; < count; i++) { params[i].worktodo(); } return true; } @override protected void onpostexecute(boolean result) { int count = this._params.length; (int = 0; < count; i++) { this._params[i].oncomplete(); } } }
Comments
Post a Comment