android - Change menu header image using AsyncTask -


so in oncreate try change image navigation view header 1 can access via url. using asynctask this:

@override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_sidebar_menu);          navigationview navigationview = (navigationview) findviewbyid(r.id.nav_view);         navigationview.setnavigationitemselectedlistener(this);          view headerview = navigationview.getheaderview(0);          user u = mainprovider.sharedinstance().getcurrentuser(this);         textview usernametext = (textview) headerview.findviewbyid(r.id.usernametext);          imageview profilepicture = (imageview)headerview.findviewbyid(r.id.profilepicture);         string profilepictureurl = u.getsettings().get("profile_picture").getasstring();         new converturltobitmap().execute(profilepictureurl);         profilepicture.setimagebitmap();     } 

and asynctask looks this:

class converturltobitmap extends asynctask<string, void, bitmap> {      private exception exception;      protected bitmap doinbackground(string... urls) {         try {             url url = new url(urls[0]);             httpurlconnection connection = (httpurlconnection) url.openconnection();             connection.setdoinput(true);             connection.connect();             inputstream input = connection.getinputstream();             bitmap mybitmap = bitmapfactory.decodestream(input);             connection.disconnect();             return mybitmap;         } catch (ioexception e) {             // log exception             return null;         }     }      protected void onpostexecute(bitmap mybitmap) {         // todo: check this.exception         // todo: feed      } } 

what fail understand how use task in setimagebitmap method:

profilepicture.setimagebitmap() 

thank time , effort!

you can create interface callback method onbitmaploaded, , use interface param of asynctask.

but know image loading can use third party libraryies picasso or glide design asynchronous image fetching, documented , easy use.

http://square.github.io/picasso/

https://github.com/bumptech/glide


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -