/** * @param origPath 源图片文件路径 * @param width 缩略图设定宽度 * @param height 缩略图设定高度 * @return 返回缩略图,失败返回null */ public Bitmap getImageThumbFromMK(String origPath, int width, int height) { // 产生缩略图,并把该文件存到指定目录下,更新数据库中图片信息 Bitmap thumbnail = null; Log.d(TAG, origPath + ":make thumbnail and insert message in database"); ThumbnailCreator mCreator = new ThumbnailCreator(width, height); thumbnail = mCreator.createThumbnail(origPath); if (thumbnail == null) { return null; } String name = null; try { name = origPath.substring(origPath.lastIndexOf("/") + 1, origPath.lastIndexOf(".")); } catch (IndexOutOfBoundsException e) { e.printStackTrace(); return null; } try { File f = new File(imageDB.getAppDir() + "/" + name); FileOutputStream fOut = null; fOut = new FileOutputStream(f); thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, fOut); fOut.flush(); fOut.close(); imageDB.insert(origPath, f.getPath()); } catch (Exception e) { // TODO Auto-generated catch block Log.d(TAG, "create temp file false"); e.printStackTrace(); } return thumbnail; }
/** 清除数据库中暂存的缩略图信息及对应的缩略图文件,需要在每次退出程序前清除过时的信息,只保留MaxThumbnailNum个缩略图信息, 防止暂存区不断扩大 */ public void clearThumbnailData() { final int ORIG_PATH_COLUMN = 0; final int THUMB_PATH_COLUMN = 1; final int CREATE_TIME = 2; String[] columns = { ImageDatabase.ORIG_PATH, ImageDatabase.THUMB_PATH, ImageDatabase.CREATE_TIME }; String sort = ImageDatabase.CREATE_TIME + " DESC"; // 按降序整理查询的结果 Cursor c = imageDB.query(columns, null, null, sort); int i = 0; if (c != null) { try { while (c.moveToNext()) { i++; // 删除第MaxThumbnailNum以后个图片及其信息 if (i > MaxThumbnailNum) { String thumbPath = c.getString(THUMB_PATH_COLUMN); String origPath = c.getString(ORIG_PATH_COLUMN); File file = new File(thumbPath); try { if (file.delete()) { imageDB.delete(origPath); } } catch (SecurityException e) { } } } } finally { c.close(); c = null; } } else { Log.d(TAG, "cursor is null"); } }
/** * 从数据库中查询返回缩略图 * * @param origPath * @return null如果查询不到该缩略图文件 */ public Bitmap getImageThumbFromDB(String origPath) { final int ORIG_PATH_COLUMN = 0; final int THUMB_PATH_COLUMN = 1; String[] columns = {ImageDatabase.ORIG_PATH, ImageDatabase.THUMB_PATH}; String selection = ImageDatabase.ORIG_PATH + "=?"; String Args[] = {origPath}; Bitmap thumbnail = null; Cursor c = imageDB.query(columns, selection, Args, null); // 如果从数据库中找到该原图的缩量图,则直接使用 if (c != null) { try { while (c.moveToNext()) { String thumbPath = c.getString(THUMB_PATH_COLUMN); thumbnail = BitmapFactory.decodeFile(thumbPath); break; } } finally { c.close(); c = null; } } return thumbnail; }
public void closeDB() { imageDB.closeDatabase(); }