public final class ImageUtils { private static final String TAG = "ImageUtils"; private static final boolean DEBUG = false; /** * 图片工具类 */ private ImageUtils() { } /** * 把图片转换成指定的大小,规则如下: * 当前图片的宽度和高度是否都大于指定高度的二倍, * 是则缩小2倍,继续判断大小 * @param f * @param width * @param height * @return * @throws IOException */ public static Bitmap decode2file(File f,final int width,final int height) throws IOException { if(f == null){ if(DEBUG) Log.i(TAG, "file is null in decode"); return null; } if (!f.exists()) { throw new IOException("local file does not exist: " + f); } if (!f.canRead()) { throw new IOException("cannot read from local file: " + f); } if(DEBUG) Log.i(TAG, ">>>>>>>>>>>>>>decode start file name : " + f.getName() + ">> width : " + width + ">>height : " + height); try { // 加载图片,获取大小 BitmapFactory.Options oc = new BitmapFactory.Options(); // 设置成 true,只获取图片大小,不会把图片加载到内存中 oc.inJustDecodeBounds = true; FileInputStream fisc = new FileInputStream(f); BitmapFactory.decodeStream(fisc, null, oc); fisc.close(); fisc = null; //计算 int tempw = oc.outWidth, temph = oc.outHeight; int scale = 1; while(true){ if(tempw / 2 < width || temph < height) break; tempw /= 2; temph /= 2; scale *= 2; } //加载指定规格图片 BitmapFactory.Options oi = new BitmapFactory.Options(); oi.inSampleSize = scale; FileInputStream fisi = new FileInputStream(f); Bitmap b = BitmapFactory.decodeStream(fisi, null, oi); fisi.close(); fisi = null; if(DEBUG) Log.i(TAG, "<<<<<<<<<<<<<<<<<< decode end"); return b; } catch (FileNotFoundException e) { if(DEBUG) Log.e(TAG, "<<<<<<<<<<<<<<not find decode file"); } catch (IOException e) { if(DEBUG) Log.e(TAG, "<<<<<<<<<<<<<io exception in decode"); e.printStackTrace(); } return null; } /** * 下载指定大小的图片 * @param url * @param width * @param height * @return */ public static Bitmap getBitmapFromNet(String url,final int width,final int height){ if(url == null){ if (DEBUG) Log.i(TAG, ">>>>>>>>>>>>download image url is null"); return null; } checkNotOnMainThead(); if(DEBUG) Log.i(TAG, ">>>>>>>>>>>download image start url: " + url); Bitmap b = null; File tempFile = null; InputStream is = null; FileOutputStream fos = null; HttpURLConnection conn = null; try{ URL imageURL = new URL(url); conn = (HttpURLConnection) imageURL.openConnection(); //连接时间,等待读取时间和重定向 conn.setConnectTimeout(20000); conn.setReadTimeout(20000); conn.setInstanceFollowRedirects(true); is = conn.getInputStream(); tempFile = new File("temp_" + MD5.encode(url)); fos = new FileOutputStream(tempFile); copyStream(is, fos); b = decode2file(tempFile, width, height); return b; }catch(Throwable tx){ if(DEBUG) Log.e(TAG, "<<<<<<<<download exception"); tx.printStackTrace(); //发生内存溢出,清理缓存 if(tx instanceof OutOfMemoryError){ if(DEBUG) Log.e(TAG, "<<<<<<<<<<<download out of memory"); } }finally{ try { if (fos != null) fos.close(); if(is != null) is.close(); conn.disconnect(); tempFile.delete(); } catch (IOException e) { e.printStackTrace(); } } return null; } /** * * @param is * @param os * @throws IOException */ public static void copyStream(InputStream is , OutputStream os) throws IOException{ final int buffSize = 1024; int count = 0; byte[] b = new byte[2 * buffSize]; while(-1 != (count = is.read(b))){ os.write(b, 0, count); } } /** * 检查是否在主线程 */ public static void checkNotOnMainThead(){ if(Looper.myLooper() == Looper.getMainLooper()){ throw new IllegalStateException("This method should not be called from the main/UI thread."); } } /** * * @param b * @param f * @throws IOException */ public static void writeBitmap2File(Bitmap b, File f) throws IOException{ if(b == null || f == null){ if(DEBUG) Log.e(TAG, "bitmap or file is null bitmap: " + b + ">>>>>file: " + f); return ; } if (!f.exists()) { throw new IOException("local file does not exist: " + f); } FileOutputStream fos = null; try{ fos = new FileOutputStream(f); b.compress(CompressFormat .PNG, 100, fos); fos.flush(); }catch(Exception ex){ if(DEBUG) Log.e(TAG, "bitmap write to file exception"); }finally{ if(fos!=null) { try { fos.close(); } catch (Exception e) { } } } } }
Android CacheImage之下载获取指定大小图片
最新推荐文章于 2024-06-28 09:21:57 发布