zoukankan      html  css  js  c++  java
  • AsyncTask简单获取网络图片的例子

    //首先呢,添加网络权限

    android.permission.INTERNET

    //然后,布局View

    img=(Image)findViewById(R.id.imageView1);

    btn=(Button)findViewById(R.id.button1);

    btn.setOnClickListener(new View.OnClickListener(){

      public void onClick(View v){

        String path=”网络图片路径”;

        new DemoTask().execute(path);

      }

    });

    //最后:创建一个类继承AsyncTask

    public class DemoTask extends AsyncTask(String, Void, Bitmap){

      //重写doInBackground来进行任务

      protected Bitmap doInBackground(String… params){

        HttpClient httpClient = new DefaultHtppClient();

        HttpGet httpGet = new HttpGet(params[0]);

        Bitmap bitmap=null;

        try{

          HttpResponse httpResponse = httpClient.execute(httpGet);

          if(httpResponse.getStatusLine().getStatusCode==200){

            //同样,在这里可以获取到很多东西

            //例如输入流:inputStream=httpResponse.getEntity().getContent();

            HttpEntity httpEntity = HttpResponse.getEntity();

            byte [] data = EntityUtils.toByteArray(httpEntity);

            bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);

          }

        }catch(Exception e){

            e.printStackTrace();

        }

        return bitmpa;

      }

      //重写onPostExecute来更新View

      protected void onPostExecute(Bitmap bm){

        super.onPostExecute(result);

        img.setImageBitmap(bm);

      }

    }

  • 相关阅读:
    P2805 [NOI2009]植物大战僵尸
    P3833 [SHOI2012]魔法树
    LOJ121 「离线可过」动态图连通性
    POJ2774 Long Long Message
    POJ2406 Power Strings
    SPOJ 694 DISUBSTR
    POJ3261 Milk Patterns
    HIHOcoder 1466 后缀自动机六·重复旋律9
    P2504 [HAOI2006]聪明的猴子
    P3804 【模板】后缀自动机
  • 原文地址:https://www.cnblogs.com/hualongbei/p/3678151.html
Copyright © 2011-2022 走看看