【Android源码 栏目提醒】:网学会员为需要Android源码 的朋友们搜集整理了ImageDownloader java android源码 - 开发文档相关资料,希望对各位网友有所帮助!
/ Copyright C 2010 The Android Open Source Project Licensed under the Apache License Version 2.0 the License you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing software distributed under the License is distributed on an AS IS BASIS WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND either express or implied. See the License for the specific language governing permissions and limitations under the License. /package com.example.android.xmladapters import org.apache.http.HttpEntity import org.apache.http.HttpResponse import org.apache.http.HttpStatus import org.apache.http.client.methods.HttpGet import android.graphics.Bitmap import android.graphics.BitmapFactory import android.graphics.Color import android.graphics.drawable.ColorDrawable import android.graphics.drawable.Drawable import android.net.http.AndroidHttpClient import android.os.AsyncTask import android.os.Handler import android.util.Log import android.widget.ImageView import java.io.BufferedOutputStream import java.io.ByteArrayOutputStream import java.io.IOException import java.io.InputStream import java.io.OutputStream import java.lang.ref.SoftReference import java.lang.ref.WeakReference import java.util.HashMap import java.util.LinkedHashMap import java.util.concurrent.ConcurrentHashMap / This helper class download images from the Internet and binds those with the provided ImageView. It requires the INTERNET permission which should be added to your applications manifest file. A local cache of downloaded images is maintained internally to improve performance. / public class ImageDownloader private static final String LOG_TAG ImageDownloader private static final int HARD_CACHE_CAPACITY 40 private static final int DELAY_BEFORE_PURGE 30 1000 // in milliseconds // Hard cache with a fixed maximum capacity and a life duration private final HashMap sHardBitmapCache new LinkedHashMapHARD_CACHE_CAPACITY / 2 0.75f true Override protected boolean removeEldestEntryLinkedHashMap.Entry eldest if size HARD_CACHE_CAPACITY // Entries push-out of hard reference cache are transferred to soft reference cache sSoftBitmapCache.puteldest.getKey new SoftReferenceeldest.getValue return true else return false // Soft cache for bitmap kicked out of hard cache private final static ConcurrentHashMap sSoftBitmapCache new ConcurrentHashMapHARD_CACHE_CAPACITY / 2 private final Handler purgeHandler new Handler private final Runnable purger new Runnable public void run clearCache / Download the specified image from the Internet and binds it to the provided ImageView. The binding is immediate if the image is found in the cache and will be done asynchronously otherwise. A null bitmap will be associated to the ImageView if an error occurs. param url The URL of the image to download. param imageView The ImageView to bind the downloaded image to. / public void downloadString url ImageView imageView downloadurl imageView null / Same as link downloadString ImageView