/** * 从文件中拿图片 * * @return */ private static Bitmap getBitmapFromFile(Context con, String url, boolean blean) { String imageName = ""; if (!url.startsWith("http")) { imageName = url.substring(url.lastIndexOf("/") + 1); } else { imageName = Utils.getMD5Str(url); } if (url.contains("ba57b8e1463b63cb01463b7a18eb0083")) { MyLogger.commLog().e(imageName); } Bitmap bitmap = null; File file = null; String cacheDir = Utils.getCacheDir(con, "picture"); if (imageName != null) { try { file = new File(cacheDir, imageName); if (file.exists()) { // DisplayMetrics dm = context.getResources().getDisplayMetrics(); // int hh = dm.heightPixels;// 这里设置高度为800f // int ww = dm.widthPixels;// 这里设置宽度为480f bitmap = decodeSampledBitmapFromFile(cacheDir + "/" + imageName, 100, 100); } } catch (Exception e) { e.printStackTrace(); bitmap = null; } } if (bitmap != null) { addBitmapToMemoryCache(url, bitmap); MyLogger.commLog().d(url); } return bitmap; }
@Override protected Bitmap doInBackground(String... params) { Bitmap data = null; if (url != null) { try { URL c_url = new URL(url); String imageName = Utils.getMD5Str(url); setBitmapToFile(imageName, c_url.openStream()); } catch (FileNotFoundException exception) { MyLogger.commLog().w("~~~~~~文件不存在~~~~~~~~·" + (url == null ? "" : url)); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return data; }
private static void createLruCache() { if (mMemoryCache != null) { return; } final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); MyLogger.commLog().e("maxMemory = " + maxMemory); // Use 1/8th of the available memory for this memory cache. final int cacheSize = maxMemory / 16; mMemoryCache = new LruCache<String, Bitmap>(cacheSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { // The cache size will be measured in kilobytes rather than // number of items. return getBitmapSize(bitmap) / 1024; } }; }