Download images from Android

Source: Internet
Author: User

Download images from Android

Method 2:

1.

public byte[] downloadResource(Context context, String url)throws ClientProtocolException, IOException {isStop = false;ByteArrayBuffer buffer = null;HttpGet hp = new HttpGet(url);httpClient = new DefaultHttpClient();String netType = isNetType(context);if (netType != null & netType.equals("cmwap")) {HttpHost proxy = new HttpHost("10.0.0.172", 80);httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY,proxy);}HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),5 * 1000);HttpConnectionParams.setSoTimeout(httpClient.getParams(), 60 * 1000);HttpResponse response = httpClient.execute(hp);if (response.getStatusLine().getStatusCode() == 200) {inputstream = response.getEntity().getContent();if (inputstream != null) {int i = (int) response.getEntity().getContentLength();buffer = new ByteArrayBuffer(1024);byte[] tmp = new byte[1024];int len;while (((len = inputstream.read(tmp)) != -1)&& (false == isStop)) {buffer.append(tmp, 0, len);}}cancel();}return buffer.toByteArray();}
Call method:

Protected Bitmap doInBackground (WonderfulprogramInfo... params) {Bitmap bitmap = null; try {String urls = Constant. url + params [0]. getWonderfulImgUrl (); boolean isExists = Files. compare (urls); if (isExists = false) {// download image data from the network. Net = new net (); byte [] data = Net. downloadResource (HomeActivity. this, urls); bitmap = BitmapFactory. decodeByteArray (data, 0, data. length); imagesCache. put (urls, bitmap); // Save the downloaded image to the cache Files. saveImage (urls, data);} else {// read Image data byte [] data = Files. readImage (urls); BitmapFactory. options opt = new BitmapFactory. options (); opt. inPreferredConfig = Bitmap. config. RGB_565; opt. inPurgeable = true; opt. ininputtransferable = true; // obtain the resource image // InputStream is = // context. getResources (). openRawResource (resId); // return BitmapFactory. decodeStream (is, null, opt); bitmap = BitmapFactory. decodeByteArray (data, 0, data. length, opt); imagesCache. put (urls, bitmap); // Save the downloaded image to the cache}

Method 2:

Class DownLoadTask extends AsyncTask
 
  
{Private ImageView imageView; private Integer positions; public DownLoadTask (ImageView view, int position) {imageView = view; this. positions = position;} protected Bitmap doInBackground (String... params) {URL url; try {url = new URL (params [0]); InputStream is = url. openStream (); BitmapFactory. options opt = new BitmapFactory. options (); opt. inPreferredConfig = Bitmap. config. RGB_565; opt. inPurgeable = true; opt. ininputretriable = true; // obtain the resource image Bitmap bitmap = BitmapFactory. decodeStream (is, null, opt); HomeActivity. topGalleryBitmap. put (positions, bitmap); return bitmap;} catch (OutOfMemoryError err) {err. printStackTrace ();} catch (MalformedURLException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} return null;} protected void onPostExecute (Bitmap result) {super. onPostExecute (result); imageView. setImageBitmap (result );}}
 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.