wemall app中基於Java擷取和儲存圖片的代碼

來源:互聯網
上載者:User

標籤:分享   shu   .exe   記憶體緩衝   下載圖片   executors   預設   pack   next   

wemall-mobile是基於WeMall的android app商城,只需要在原商城目錄下上傳介面檔案即可完成服務端的配置,用戶端可定製修改。分享其中關於 儲存正在下載的圖片URL集合和圖片三種擷取方式管理者,網路URL擷取、記憶體緩衝擷取、外部檔案快取擷取的代碼供技術員學習參考使用。

 

package com.inuoer.util;import java.lang.ref.SoftReference;import java.util.HashMap;import java.util.HashSet;import java.util.Map;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import android.content.Context;import android.graphics.Bitmap;import android.os.Handler;public class AsyncImageLoader {// 儲存正在下載的圖片URL集合,避免重複下載用private static HashSet<String> sDownloadingSet;// 軟引用記憶體緩衝private static Map<String, SoftReference<Bitmap>> sImageCache;// 圖片三種擷取方式管理者,網路URL擷取、記憶體緩衝擷取、外部檔案快取擷取private static LoaderImpl impl;// 線程池相關private static ExecutorService sExecutorService;// 通知UI線程圖片擷取ok時使用private Handler handler;/** * 非同步載入圖片完畢的回調介面 */public interface ImageCallback {/** * 回呼函數 *  * @param bitmap *            : may be null! * @param imageUrl */public void onImageLoaded(Bitmap bitmap, String imageUrl);}static {sDownloadingSet = new HashSet<String>();sImageCache = new HashMap<String, SoftReference<Bitmap>>();impl = new LoaderImpl(sImageCache);}public AsyncImageLoader(Context context) {handler = new Handler();startThreadPoolIfNecessary();String defaultDir = context.getCacheDir().getAbsolutePath();setCachedDir(defaultDir);}/** * 是否緩衝圖片至/data/data/package/cache/目錄 預設不緩衝 */public void setCache2File(boolean flag) {impl.setCache2File(flag);}/** * 設定緩衝路徑,setCache2File(true)時有效 */public void setCachedDir(String dir) {impl.setCachedDir(dir);}/** 開啟線程池 */public static void startThreadPoolIfNecessary() {if (sExecutorService == null || sExecutorService.isShutdown()|| sExecutorService.isTerminated()) {sExecutorService = Executors.newFixedThreadPool(3);// sExecutorService = Executors.newSingleThreadExecutor();}}/** * 非同步下載圖片,並緩衝到memory中 *  * @param url * @param callback *            see ImageCallback interface */public void downloadImage(final String url, final ImageCallback callback) {downloadImage(url, true, callback);}/** *  * @param url * @param cache2Memory *            是否緩衝至memory中 * @param callback */public void downloadImage(final String url, final boolean cache2Memory,final ImageCallback callback) {if (sDownloadingSet.contains(url)) {//Log.i("AsyncImageLoader", "###該圖片正在下載,不能重複下載!");return;}Bitmap bitmap = impl.getBitmapFromMemory(url);if (bitmap != null) {if (callback != null) {callback.onImageLoaded(bitmap, url);}} else {// 從網路端下載圖片sDownloadingSet.add(url);sExecutorService.submit(new Runnable() {@Overridepublic void run() {final Bitmap bitmap = impl.getBitmapFromUrl(url,cache2Memory);handler.post(new Runnable() {@Overridepublic void run() {if (callback != null)callback.onImageLoaded(bitmap, url);sDownloadingSet.remove(url);}});}});}}/** * 預先載入下一張圖片,緩衝至memory中 *  * @param url */public void preLoadNextImage(final String url) {// 將callback置為空白,只將bitmap緩衝到memory即可。downloadImage(url, null);}}

  

wemall-mobile詳情:http://www.koahub.com/home/product/56

wemall官網地址:http://www.wemallshop.com

wemall 開源微商城 ,商城,商城源碼,三級分銷,微生鮮,微水果,微外賣,微訂餐---專業的o2o系統

wemall app中基於Java擷取和儲存圖片的代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.