Skip to content

Instantly share code, notes, and snippets.

@januprasad
Created December 3, 2014 09:28
Show Gist options
  • Save januprasad/c1f605156e074e08c877 to your computer and use it in GitHub Desktop.
Save januprasad/c1f605156e074e08c877 to your computer and use it in GitHub Desktop.
AppDataPref
public class AppDataPref {
public static final String TEMP_PHOTO_FILE_NAME = "fbprofileimg.jpg";
private String DIRECTORY_NAME = "SocialNetGate";
public static boolean CALL_USER_DETAILS = false;
public static boolean CALL_INFLUENCE_CALC = true;
private Context context;
private SharedPreferences prefs;
public SharedPreferences prefs1;
public AppDataPref(Context context) {
this.context = context;
prefs = context.getSharedPreferences("com.ephron.sngtab.sha",
Context.MODE_PRIVATE);
prefs1 = context.getSharedPreferences("com.ephron.sngtab.sha1",
Context.MODE_PRIVATE);
System.out.println(".........");
}
public String imageViewProfilePhoto(String name) {
return prefs.getString(name, "");
}
public String getPrefs(String name) {
return prefs.getString(name, "");
}
public void setPrefs(String key, String value) {
prefs.edit().putString(key, value).commit();
}
public String getPrefs1(String name) {
return prefs1.getString(name, "");
}
public void setPrefs1(String key, String value) {
prefs1.edit().putString(key, value).commit();
}
public static boolean deleteDirectory(File path) {
if (path.exists()) {
File[] files = path.listFiles();
if (files == null) {
return true;
}
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
deleteDirectory(files[i]);
} else {
files[i].delete();
}
}
}
return (path.delete());
}
public void clear() {
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
DIRECTORY_NAME);
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
}
}
try {
context.getContentResolver()
.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
MediaStore.Images.Media.DATA + "='"
+ mediaStorageDir + "'", null);
} catch (Exception e) {
e.printStackTrace();
}
deleteDirectory(mediaStorageDir);
prefs.edit().clear().commit();
}
public Bitmap getprofileImage() {
return decodeBase64(prefs.getString("PROFILE_IMAGE", null));
}
public void setprofileImage(String profileImage, Bitmap yourbitmap) {
Editor editor = prefs.edit();
editor.putString("PATH_PROFILE_IMAGE", profileImage);
editor.putString("PROFILE_IMAGE", encodeTobase64(yourbitmap));
editor.commit();
}
public static Bitmap decodeBase64(String input) {
if (input.length() == 0)
return null;
byte[] decodedByte = Base64.decode(input, 0);
return BitmapFactory
.decodeByteArray(decodedByte, 0, decodedByte.length);
}
public static String encodeTobase64(Bitmap image) {
Bitmap immage = image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
immage.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
Log.i("Image Log:", imageEncoded);
return imageEncoded;
}
// public void setList(String string, Set<String> set) {
// Editor editor = prefs.edit();
// editor.putStringSet(string, set);
// editor.commit();
// }
public void putList(String key, ArrayList<String> marray) {
SharedPreferences.Editor editor = prefs.edit();
String[] mystringlist = marray.toArray(new String[marray.size()]);
// the comma like character used below is not a comma it is the SINGLE
// LOW-9 QUOTATION MARK unicode 201A and unicode 2017 they are used for
// seprating the items in the list
editor.putString(key, TextUtils.join("‚‗‚", mystringlist));
editor.apply();
}
public ArrayList<String> getList(String key) {
// the comma like character used below is not a comma it is the SINGLE
// LOW-9 QUOTATION MARK unicode 201A and unicode 2017 they are used for
// seprating the items in the list
String[] mylist = TextUtils.split(prefs.getString(key, ""), "‚‗‚");
ArrayList<String> gottenlist = new ArrayList<String>(
Arrays.asList(mylist));
return gottenlist;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment