Setting bitmap from url as home icon for taskbar in android

I want to download an image from a URL and set it as the home icon in my action bar at runtime. I use AsyncTask to execute it, but it does not seem to change it. Any ideas?

class getProfilePicture extends AsyncTask<Void, Void, Void> { protected Void doInBackground(Void... params) { try { URL url; url = new URL("http://www.i2clipart.com/cliparts/2/a/3/2/clipart-fcrc-logo-handshake-2a32.png"); HttpURLConnection conn = (HttpURLConnection) url .openConnection(); conn.setDoInput(true); conn.connect(); InputStream is = conn.getInputStream(); image = BitmapFactory.decodeStream(is); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } protected void onPostExecute() { // TODO: check this.exception Resources res = getResources(); BitmapDrawable icon = new BitmapDrawable(res, image); getSupportActionBar().setIcon(icon); //getSupportActionBar().setLogo(icon); } } 
+4
source share
1 answer

you can try this kotlin code

 Glide.with(this).load(imageUrl).asBitmap().into(object : SimpleTarget<Bitmap>() { override fun onResourceReady(resource: Bitmap?, glideAnimation: GlideAnimation<in Bitmap>?) { val drawable = BitmapDrawable(resources, resource) supportActionBar?.setIcon(drawable) } }) 
0
source

Source: https://habr.com/ru/post/1491207/


All Articles