今天分享一个自己一直在使用SharedPreferences工具类,几乎可以满足开发中的全部需求了。
public class SPUtils {
public static void saveBitmapToSharedPreferences(Context context, String preference,String key, Bitmap bitmap){
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 80, byteArrayOutputStream);
byte[] byteArray=byteArrayOutputStream.toByteArray();
String imageString=new String(Base64.encodeToString(byteArray, Base64.DEFAULT));
SharedPreferences sharedPreferences = context.getSharedPreferences(preference, Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putString(key, imageString);
editor.commit();
}
public static Bitmap getBitmapFromSharedPreferences(Context context,String preference, String key, String defaultValue){
SharedPreferences sharedPreferences = context.getSharedPreferences(preference, Context.MODE_PRIVATE);
String imageString=sharedPreferences.getString(key,defaultValue);
if (imageString!=null){
byte[] byteArray= Base64.decode(imageString, Base64.DEFAULT);
ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(byteArray);
return BitmapFactory.decodeStream(byteArrayInputStream);
}else{
return null;
}
}
public static void setStringPreferences(Context context, String preference,String key, String value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(preference, Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
public static String getStringPreference(Context context,String preference, String key, String defaultValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences(preference, Context.MODE_PRIVATE);
return sharedPreferences.getString(key, defaultValue);
}
public static void setLongPreference(Context context, String preference,String key, long value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(preference, Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putLong(key, value);
editor.commit();
}
public static long getLongPreference(Context context, String preference,String key, long defaultValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences(preference, Context.MODE_PRIVATE);
return sharedPreferences.getLong(key, defaultValue);
}
public static void setBooleanPreferences(Context context,String preference, String key, boolean value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(preference, Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
public static boolean getBooleanPreference(Context context,String preference, String key, boolean defaultValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences(preference, Context.MODE_PRIVATE);
return sharedPreferences.getBoolean(key, defaultValue);
}
public static void setIntPreferences(Context context, String preference,String key, int value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(preference, Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putInt(key, value);
editor.commit();
}
public static int getIntPreference(Context context, String preference,String key, int defaultValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences(preference, Context.MODE_PRIVATE);
return sharedPreferences.getInt(key, defaultValue);
}
public static void deletePrefereceKey(Context context, String preference,String key) {
SharedPreferences sharedPreferences = context.getSharedPreferences(preference, Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.remove(key);
editor.commit();
}
}