1. 程式人生 > >顯示圖片

顯示圖片

class rri mipmap new timeout round cnblogs protected param

    private void loadImage(String url, final ImageView ivIcon){


        new AsyncTask<String,Void,Bitmap>(){

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                super.onPostExecute(bitmap);

                if(bitmap == null){
                    ivIcon.setImageResource(R.mipmap.ic_launcher);
                }
else{ ivIcon.setImageBitmap(bitmap); } } @Override protected Bitmap doInBackground(String... params) { try { URL url = new URL(params[0]); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(
"GET"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); int code = connection.getResponseCode(); if(code == 200){ InputStream is = connection.getInputStream();
return BitmapFactory.decodeStream(is); } } catch (Exception e) { e.printStackTrace(); } return null; } }.execute(url);

顯示圖片