5

我想从 url 下载图像并在运行时将其设置为我的操作栏中的主页图标。我正在使用 AsyncTask 来执行它,但它似乎并没有改变它。有任何想法吗?

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

1 回答 1

0

你可以试试这个 kotlin 代码

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)
                    }
                })
于 2017-07-20T10:18:48.600 回答