Android下載圖片 圖片的非同步載入 和緩衝存取,

來源:互聯網
上載者:User

Android下載圖片 圖片的非同步載入 和緩衝存取,

一、建立非同步任務

public class LoadBitmapAsyn extends AsyncTask<String,Void,Bitmap> {    Context context;    ImageView img;    private HashMap<String,SoftReference<Bitmap>> imageCache=null;    public LoadBitmapAsyn(ImageView img){        this.img=img;        this.context=context;    }    @Override    protected void onPreExecute() {        super.onPreExecute();        imageCache=new HashMap<String,SoftReference<Bitmap>>();    }    @Override    protected Bitmap doInBackground(String... strings) {            try {
//擷取到網路資料,返回bitmap                InputStream in = new URL(strings[0]).openStream();                Bitmap bitmap1 = BitmapFactory.decodeStream(in);//儲存到手機記憶體的指定位置                File dir = new File(Environment.getExternalStorageDirectory() + "/test/");                if (!dir.exists()) {                    dir.mkdirs();                }                File bitmapFile = new File(Environment.getExternalStorageDirectory() + "/test/" +                        strings[0].substring(strings[0].lastIndexOf("/") + 1));                if (!bitmapFile.exists()) {                    try {                        bitmapFile.createNewFile();                    } catch (IOException e) {//TODOAuto-generatedcatchblock                        e.printStackTrace();                    }                }                FileOutputStream fos;                try {                    fos = new FileOutputStream(bitmapFile);                    bitmap1.compress(Bitmap.CompressFormat.PNG,                            100, fos);                    fos.close();                } catch (FileNotFoundException e) {                    e.printStackTrace();                } catch (IOException e) {                    e.printStackTrace();                }                return bitmap1;            } catch (Exception e) {                int i = 1 + 1;            }        return null;    }    @Override    protected void onPostExecute(Bitmap bitmap) {        super.onPostExecute(bitmap);        img.setImageBitmap(bitmap);    }}二、activity中檢測緩衝中是否有蓋圖片,優先載入記憶體緩衝,其次是內部儲存,最後才是網上下載

//檢測緩衝和記憶體中是否有指定路徑名的bitmap資料

 public Bitmap loadBitmap(final String imageURL){        imageCache=new HashMap<String,SoftReference<Bitmap>>();//在記憶體緩衝中,則返回Bitmap對象        if (imageCache.containsKey(imageURL)) {            SoftReference<Bitmap> reference = imageCache.get(imageURL);            Bitmap bitmap = reference.get();            if (bitmap != null) {                return bitmap;            }        } else {/** *加上一個對本機快取的尋找 */
//提取圖片名字            String bitmapName = imageURL.substring(imageURL.lastIndexOf("/") + 1);            File cacheDir = new File(Environment.getExternalStorageDirectory()+"/test/");            File[] cacheFiles = cacheDir.listFiles();            int i = 0;            if (null != cacheFiles) {                for (; i < cacheFiles.length; i++) {                    if (bitmapName.equals(cacheFiles[i].getName())) {                        break;                    }                }                if (i < cacheFiles.length) {                    return BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/test/" + bitmapName);                }            }        }        return null;    }

三、activity中的調用

Bitmap bitmap = loadBitmap(url);
ImageView img = new ImageView(yourActivity);
if (bitmap2 != null) {    img.setImageBitmap(bitmap2);} else {    LoadBitmapAsyn loadBitmapAsyn = new LoadBitmapAsyn(imageView);    loadBitmapAsyn.execute(url);}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.