安卓開發通常都須要進行數據緩存,經常使用操做老司機已爲你封裝完畢,常常有小夥伴問怎麼判斷緩存是否可用,那我告訴你,你能夠用這份工具進行存儲和查詢,具體能夠查看源碼,如今爲你開車,Demo傳送門。
站點java
put : 保存數據的方法 get : 獲取數據的方法 putImage: 保存圖片到SharedPreferences getImage: 從SharedPreferences讀取圖片 remove : 移除某個key值已經對應的值 clear : 清除全部數據 contains: 查詢某個key是否已經存在 getAll : 返回全部的鍵值對
public class AppSharePreferenceMgr {git
/** * 保存在手機裏面的文件名 */ public static final String FILE_NAME = "share_data"; /** * 保存數據的方法,咱們須要拿到保存數據的具體類型,而後根據類型調用不一樣的保存方法 */ public static void put(Context context, String key, Object object) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); if (object instanceof String) { editor.putString(key, (String) object); } else if (object instanceof Integer) { editor.putInt(key, (Integer) object); } else if (object instanceof Boolean) { editor.putBoolean(key, (Boolean) object); } else if (object instanceof Float) { editor.putFloat(key, (Float) object); } else if (object instanceof Long) { editor.putLong(key, (Long) object); } else { editor.putString(key, object.toString()); } SharedPreferencesCompat.apply(editor); } /** * 獲得保存數據的方法,咱們根據默認值獲得保存的數據的具體類型,而後調用相對於的方法獲取值 */ public static Object get(Context context, String key, Object defaultObject) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); if (defaultObject instanceof String) { return sp.getString(key, (String) defaultObject); } else if (defaultObject instanceof Integer) { return sp.getInt(key, (Integer) defaultObject); } else if (defaultObject instanceof Boolean) { return sp.getBoolean(key, (Boolean) defaultObject); } else if (defaultObject instanceof Float) { return sp.getFloat(key, (Float) defaultObject); } else if (defaultObject instanceof Long) { return sp.getLong(key, (Long) defaultObject); } return null; } /** * 移除某個key值已經對應的值 */ public static void remove(Context context, String key) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.remove(key); SharedPreferencesCompat.apply(editor); } /** * 清除全部數據 */ public static void clear(Context context) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.clear(); SharedPreferencesCompat.apply(editor); } /** * 查詢某個key是否已經存在 */ public static boolean contains(Context context, String key) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); return sp.contains(key); } /** * 返回全部的鍵值對 */ public static Map<String, ?> getAll(Context context) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); return sp.getAll(); }
/** * 保存圖片到SharedPreferences * @param mContext * @param imageView */ public static void putImage(Context mContext, String key, ImageView imageView) { BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable(); Bitmap bitmap = drawable.getBitmap(); // 將Bitmap壓縮成字節數組輸出流 ByteArrayOutputStream byStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 80, byStream); // 利用Base64將咱們的字節數組輸出流轉換成String byte[] byteArray = byStream.toByteArray(); String imgString = new String(Base64.encodeToString(byteArray, Base64.DEFAULT)); // 將String保存shareUtils AppSharePreferenceMgr.put(mContext, key, imgString); } /** * 從SharedPreferences讀取圖片 * @param mContext * @param imageView */ public static Bitmap getImage(Context mContext, String key, ImageView imageView) { String imgString = (String) AppSharePreferenceMgr.get(mContext, key, ""); if (!imgString.equals("")) { // 利用Base64將咱們string轉換 byte[] byteArray = Base64.decode(imgString, Base64.DEFAULT); ByteArrayInputStream byStream = new ByteArrayInputStream(byteArray); // 生成bitmap return BitmapFactory.decodeStream(byStream); } return null; } /** * 建立一個解決SharedPreferencesCompat.apply方法的一個兼容類 */ private static class SharedPreferencesCompat { private static final Method sApplyMethod = findApplyMethod(); /** * 反射查找apply的方法 */ @SuppressWarnings({ "unchecked", "rawtypes" }) private static Method findApplyMethod() { try { Class clz = SharedPreferences.Editor.class; return clz.getMethod("apply"); } catch (NoSuchMethodException e) { } return null; } /** * 若是找到則使用apply執行,不然使用commit */ public static void apply(SharedPreferences.Editor editor) { try { if (sApplyMethod != null) { sApplyMethod.invoke(editor); return; } } catch (IllegalArgumentException e) { } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { } editor.commit(); } }
}github
終點站
好了,終點站到了,若是對本次旅途滿意的話,請給五星好評哦,沒關注的小夥伴輕輕點個上方的關注,畢竟老司機犧牲了不少時間才換來這麼一份工具類,若是該工具類依賴其餘工具類,均可以在個人史上最全的經常使用開發工具類收集(持續更新中)中找到。數組