AsyncTask获取网络图片

来源:互联网 发布:nginx 重启 windows 编辑:程序博客网 时间:2024/06/08 14:20

我后期做学校教务系统android版的网络优化,想到用AsyncTask获取学生头像,初期,我是直接在Main中在主线程用new Thread,然后在里面添加任务获取,但是有点不好的地方是,没有进度条。因此我用了AsyncTask。

总结:输出结果可以不只是字符串,只需要在继承AsyncTask时声明就行

        public class touxiang extends Activity {    private Button button2;    private ProgressBar progressBar;    private ImageView touxiang;    private MTask mTask;    private static final String ACTIVITY_TAG = "LogDemo";    private URL url = null;    String True_Cookie = null;    String touxiang_Url = "http://jw.jc.nuaa.edu.cn/Manager/Module/NetEa/Controls/ImageHandler.ashx?ImageName=";    String xuehao;    String touxiang_Url2 = "&KeyWord=00&BinaryType=SchoolRoll&SaveType=Disk&ImageFilePath=";    byte[] data;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_touxiang);        touxiang = (ImageView) findViewById(R.id.touxiang_im);        progressBar = (ProgressBar) findViewById(R.id.Pro);        Intent intent = getIntent();        True_Cookie = intent.getStringExtra("True_Cookie");        xuehao = intent.getStringExtra("xuehao");        mTask = new MTask();//获取图片数据        mTask.execute(touxiang_Url + xuehao + touxiang_Url2);    }        //第一个字符串是网络地址,第二个是用于进度条由于我简化了就没用,第三个是完成后的输出结果,我用的字节数组。        public class MTask extends AsyncTask<String, Integer, byte[]> {        @Override        protected void onPreExecute() {            Log.v(ACTIVITY_TAG, "onPreExecute");        }        @Override        protected void onPostExecute(byte[] result) {            Log.i(ACTIVITY_TAG, "onPostExecute"+result);            progressBar.setVisibility(View.GONE);      //这是我进度的图片,加载完后隐藏            Bitmap bitmap = BitmapFactory.decodeByteArray(result, 0, result.length);            touxiang.setImageBitmap(bitmap);//显示图片        }                @Override        protected byte[] doInBackground(String... strings) {<span style="white-space:pre"></span>    //由于学校教务网是需要Cookie才能访问图片,因此稍微麻烦点,需要把数据包打包然后获得返回数据            try {                byte b[] = null;                URL url = new URL(strings[0]);                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();                urlConnection.setRequestMethod("GET");                urlConnection.setConnectTimeout(10 * 1000);                urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (MSIE 9.0; Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko");                urlConnection.setRequestProperty("Connection", "Keep-Alive");                urlConnection.setRequestProperty("Cookie", True_Cookie);                Log.v(ACTIVITY_TAG, "touxiang-----" + urlConnection.getResponseCode());                if (urlConnection.getResponseCode()== 200) {                    InputStream inputStream = urlConnection.getInputStream();                    b = StreamTool(inputStream);                    inputStream.close();                }                return b;            } catch (Exception e) {                e.printStackTrace();            }            return null;        }    }    public byte[] StreamTool(InputStream inputStream){        int len = -1;        byte buf[] = new byte[1024];        ByteArrayOutputStream out = new ByteArrayOutputStream();        try {            while((len=inputStream.read(buf))!=-1)            {                out.write(buf,0,len);            }            out.close();            return out.toByteArray();        } catch (IOException e) {            e.printStackTrace();        }        return null;    }
0 0
原创粉丝点击