本身寫的簡易網絡圖片加載工具類,可能存在些問題,歡迎指正java
/** * 圖片加載器 * * @author HYJ * */ public class ImgLoaderUtil { private Map<String, SoftReference<Bitmap>> imgCache = new LinkedHashMap<String, SoftReference<Bitmap>>(); private static volatile ImgLoaderUtil imgLoader = null; private final static String PATH = "mnt/sdcard/imageCache/"; private ImgLoaderUtil() { } public static ImgLoaderUtil getLoader() { if (imgLoader == null) { synchronized (ImgLoaderUtil.class) { if (imgLoader == null) { imgLoader = new ImgLoaderUtil(); } } } return imgLoader; } public Map<String, SoftReference<Bitmap>> getImgCache() { return imgCache; } private ImageManagerutils manager = new ImageManagerutils(); public static ExecutorService threadPool = Executors.newFixedThreadPool(5); public synchronized Bitmap loadImg(final String url, final ImgCallback callback, final Handler handler) { // 先從緩存中讀取圖片資源 Bitmap bitmap = null; try { bitmap = manager.getImgFromCache(url); if (null == bitmap) { // 開啓線程從網絡上下載 threadPool.submit(new Runnable() { // submit方法確保下載是從線程池中的線程執行 @Override public void run() { final Bitmap bitmapFromUrl = manager.getBitMapFromUrl(url); try { writePic2SDCard(bitmapFromUrl, url); } catch (Exception e) { e.printStackTrace(); } handler.post(new Runnable() { @Override public void run() { // TODO Auto-generated method stub if (callback != null) { callback.refresh(bitmapFromUrl); } } }); } }); } else { // } } catch (Exception e) { e.printStackTrace(); } return bitmap; } public interface ImgCallback { public void refresh(Bitmap bitmap); } class ImageManagerutils { /** * 從網絡上下載 * * @param url * @return */ public Bitmap getBitMapFromUrl(String url) { Bitmap bitmap = null; URL u = null; HttpURLConnection conn = null; InputStream is = null; try { u = new URL(url); conn = (HttpURLConnection) u.openConnection(); is = conn.getInputStream(); bitmap = BitmapFactory.decodeStream(is); } catch (Exception e) { e.printStackTrace(); } return bitmap; } /* * 得到設置信息 */ public Options getOptions(String path){ Options options = new Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); return options; } /** * 從文件中讀取 * * @return * @throws Exception */ private Bitmap getBitMapFromSDCard(String url) throws Exception { Bitmap bitmap = null; File file = new File(PATH + base64String(url)); if (!file.exists()) { return null; } BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; opts.inPreferredConfig = Bitmap.Config.RGB_565; InputStream fis = new FileInputStream(file); try { bitmap = BitmapFactory.decodeStream(fis); } catch (Exception e) { return null; } return bitmap; } public byte[] readStream(InputStream inStream) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } outStream.close(); inStream.close(); return outStream.toByteArray(); } /** * 從緩存中讀取 * * @param url * @return * @throws Exception */ public Bitmap getImgFromCache(String url) throws Exception { Bitmap bitmap = null; // 從內存中讀取 if (imgCache.containsKey(base64String(url))) { synchronized (imgCache) { SoftReference<Bitmap> bitmapReference = imgCache.get(base64String(url)); if (null != bitmapReference) { bitmap = bitmapReference.get(); } } // 不然從文件中讀取 } else if (null != (bitmap = getBitMapFromSDCard(url))) { // 將圖片保存進內存中 imgCache.put(base64String(url), new SoftReference<Bitmap>(bitmap)); } return bitmap; } } // public void removeImgFromCatche(String url) { // if (imgCache.containsKey(base64String(url))) { // Bitmap imageBit = imgCache.get( // Base64.encodeToString(url.getBytes(), Base64.DEFAULT)) // .get(); // if (imageBit != null && !imageBit.isRecycled()) { // imageBit.recycle(); // imgCache.remove(base64String(url)); // imageBit = null; // } // } // } /** * 將圖片寫入sdcard中 * * @param bitmap * @param url * @throws Exception */ public static final File writePic2SDCard(Bitmap bitmap, String url) throws Exception { File folder = new File(PATH); if (!folder.exists()) { folder.mkdir(); } String filename = PATH + base64String(url); File file = new File(filename); if (!file.exists()) { file.createNewFile(); } FileOutputStream fos = null; ByteArrayInputStream bis = null; try { fos = new FileOutputStream(file); byte[] bitmapByte = bitmap2Byte(bitmap); bis = new ByteArrayInputStream(bitmapByte); int len = 0; byte[] b = new byte[bis.available()]; while ((len = bis.read(b)) != -1) { fos.write(b, 0, len); } } catch (Exception e) { return null; } finally { if (null != bis) { bis.close(); } if (null != fos) { fos.close(); } } return file; } /** * bitMap 轉化爲數組 * * @param bitmap * @return */ public static final byte[] bitmap2Byte(Bitmap bitmap) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); } private static String base64String(String url) { return Base64.encodeToString(url.getBytes(), Base64.DEFAULT); } }
圖片處理方法數組
/** * 自適應切割縮放工具<br/> * 不管你提供的是大圖小圖,是高的仍是寬的,都能根據提供的目標尺寸進行「最合適的」切割縮放 * @param srcBMP - 原圖 * @param dstWidth - 目標寬度 * @param dstHeight - 目標高度 * @return - 切割縮放完後的Bitmap對象 */ public static final Bitmap adaptive(Bitmap srcBMP, int dstWidth, int dstHeight) { Matrix matrix = new Matrix(); final int srcWidth = srcBMP.getWidth(); final int srcHeight = srcBMP.getHeight(); final float srcRatio = ((float) srcHeight) / srcWidth; final float dstRatio = ((float) dstHeight) / dstWidth; if (srcRatio < dstRatio){ final float scale = ((float) dstHeight) / srcHeight; matrix.postScale(scale, scale); final float width = dstWidth / scale; return Bitmap.createBitmap(srcBMP, (int) ((srcWidth - width) / 2), 0, (int) width, srcHeight, matrix, true); } else if (srcRatio > dstRatio) { final float scale = ((float) dstWidth) / srcWidth; matrix.postScale(scale, scale); final float height = dstHeight / scale; return Bitmap.createBitmap(srcBMP, 0, (int) ((srcHeight - height) / 2), srcWidth, (int) height, matrix, true); } else { final float scale = ((float) dstWidth) / srcWidth; matrix.postScale(scale, scale); return Bitmap.createBitmap(srcBMP, 0, 0, srcWidth, srcHeight, matrix, true); } }