A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© y1787257661 中级黑马   /  2015-1-6 10:40  /  1269 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. class DownLoadItemImageAsync extends
  2.             AsyncTask<TaobaokeItem, Integer, Bitmap> {

  3.         private TaobaokeItem item;
  4.      
  5.         @Override
  6.         protected Bitmap doInBackground(TaobaokeItem... params) {
  7.             item = params[0];
  8.             Bitmap bitmap = getBitmap();
  9.             if (bitmap != null) {
  10.                 return bitmap;
  11.             }
  12.             return getItemPic(item.getPicUrl());
  13.         }

  14.         @Override
  15.         protected void onPostExecute(Bitmap result) {
  16.          
  17.             Drawable drawable = new BitmapDrawable(result);
  18.             setProgressBarIndeterminateVisibility(false);
  19.             mImageSwitcher.setImageDrawable(drawable);

  20.         }

  21.         private Bitmap getItemPic(String picUrl) {
  22.             URL url = null;
  23.             URLConnection conn = null;
  24.             InputStream in = null;
  25.             Bitmap itemBitmap = null;
  26.             try {
  27.                 url = new URL(picUrl);
  28.                 conn = url.openConnection();
  29.                 conn.setConnectTimeout(3 * 1000);
  30.                 conn.connect();

  31.                 in = conn.getInputStream();

  32.                 itemBitmap = BitmapFactory.decodeStream(in);
  33.                 storeInSD(itemBitmap);
  34.             } catch (MalformedURLException e) {
  35.                 e.printStackTrace();
  36.             } catch (IOException e) {
  37.                 e.printStackTrace();
  38.             } finally {
  39.                 if (url != null) {
  40.                     url = null;
  41.                 }
  42.                 if (conn != null) {
  43.                     conn = null;
  44.                 }
  45.                 if (in != null) {
  46.                     try {
  47.                         in.close();
  48.                     } catch (IOException e) {
  49.                         e.printStackTrace();
  50.                     }
  51.                 }
  52.             }
  53.             return itemBitmap;
  54.         }

  55.         private void storeInSD(Bitmap bitmap) {
  56.             File file = new File(FILE_DIR);
  57.             if (!file.exists()) {
  58.                 file.mkdir();
  59.             }
  60.             File imageFile = new File(file, item.getNumIid() + ".jpg");
  61.             try {
  62.                 imageFile.createNewFile();
  63.                 FileOutputStream fos = new FileOutputStream(imageFile);
  64.                 bitmap.compress(CompressFormat.JPEG, 50, fos);
  65.                 fos.flush();
  66.                 fos.close();
  67.             } catch (FileNotFoundException e) {
  68.                 // TODO Auto-generated catch block
  69.                 e.printStackTrace();
  70.             } catch (IOException e) {
  71.                 // TODO Auto-generated catch block
  72.                 e.printStackTrace();
  73.             }
  74.         }

  75.         private Bitmap getBitmap() {
  76.             File imageFile = new File(FILE_DIR, item.getNumIid() + ".jpg");
  77.             Bitmap bitmap = null;
  78.             if (imageFile.exists()) {
  79.                 try {
  80.                     bitmap = BitmapFactory.decodeStream(new FileInputStream(
  81.                             imageFile));
  82.                 } catch (FileNotFoundException e) {
  83.                     // TODO Auto-generated catch block
  84.                     e.printStackTrace();
  85.                 }
  86.             }

  87.             return bitmap;
  88.         }
  89.     }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马