/** * 从文件中拿图片 * * @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; }
public ImageDownloader(Context context) { super(); ImageDownloader.context = context; cacheDirPath_ = Utils.getCacheDir(context, cacheDirName); if (!TextUtils.isEmpty(cacheDirPath_)) { File cachedir = new File(cacheDirPath_); if (!cachedir.exists()) { cachedir.mkdirs(); } } if (mMemoryCache == null) { createLruCache(); } }
/** 将下载好的图片存放到文件中 */ private void setBitmapToFile(String imageName, InputStream is) { File file = null; FileOutputStream fos = null; try { file = new File(cacheDirPath_, imageName); if (!file.exists()) { File file2 = new File(cacheDirPath_); file2.mkdirs(); } if (Utils.hasSDCard()) { fos = new FileOutputStream(file); } else { fos = context.openFileOutput(cacheDirName + "/" + imageName, Context.MODE_PRIVATE); } int len = -1; byte[] buffer = new byte[1024]; while ((len = is.read(buffer)) != -1) { fos.write(buffer, 0, len); fos.flush(); } } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 100, 100); if (null != bitmap) { Bitmap compressBitmap = BitmapUtil.compressImage(bitmap); String cachePath = Utils.getCacheDir(context, "picture"); setBitmapToFile(cachePath, imageName, compressBitmap); } } }