Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving byte array using SharedPreferences

So I have a byte [] array which I have created in my android app. I want to use SharedPreferences from android to store it and retrieve it back again when I start my app. How can I do that ?

like image 254
user2453055 Avatar asked Oct 24 '13 03:10

user2453055


People also ask

Can we store array in SharedPreferences?

You can save String and custom array list using Gson library. =>First you need to create function to save array list to SharedPreferences. public void saveListInLocal(ArrayList<String> list, String key) { SharedPreferences prefs = getSharedPreferences("AppName", Context. MODE_PRIVATE); SharedPreferences.

How do I save something in SharedPreferences?

Shared Preferences allow you to save and retrieve data in the form of key,value pair. In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.

Is SharedPreferences thread safe?

The SharedPreferences implementation in Android is thread-safe but not process-safe. Normally your app will run all in the same process, but it's possible for you to configure it in the AndroidManifest.

Can we store objects in SharedPreferences?

We can store fields of any Object to shared preference by serializing the object to String.


3 Answers

You can save a byte array in SharedPreferences by using android.util.Base64.

For saving:

String saveThis = Base64.encodeToString(array, Base64.DEFAULT); 

For loading:

byte[] array = Base64.decode(stringFromSharedPrefs, Base64.DEFAULT); 
like image 103
Daniel Landau Avatar answered Sep 19 '22 03:09

Daniel Landau


You actually enlarge the size of a data when you convert it to a Base64 String.

the final size of Base64-encoded binary data is equal to 1.37 times the original data size + 814 bytes (for headers).

It's faster and memory efficient to save a byte[] in the SharedPreferences using Charsets.ISO_8859_1

private static final String PREF_NAME = "SharedPreferences_Name"; private static final String DATA_NAME = "BytesData_Name";  public static byte[] getBytes(Context ctx) {     SharedPreferences prefs = ctx.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);     String str = prefs.getString(DATA_NAME, null);     if (str != null) {         return str.getBytes(Charsets.ISO_8859_1);     }     return null; }  public static void setBytes(Context ctx, byte[] bytes) {     SharedPreferences prefs = ctx.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);     SharedPreferences.Editor e = prefs.edit();     e.putString(DATA_NAME, new String(bytes, Charsets.ISO_8859_1));     e.commit(); } 
  • ISO_8859_1 Preserves your data (unlike UTF-8 and UTF-16)
  • If you are going to transfer these bytes outside the app, using a JSON for example, then you will have to convert the byte[] to Base64 before serializing them.
  • JSON won't be able to understand the weird characters ISO_8859_1 will be using.

TIP : if you want to save on more space (in case your saving huge byte[]) compress the byte[] before you convert it to any format (ISO or Base64)

like image 44
Ariel Yust Avatar answered Sep 18 '22 03:09

Ariel Yust


You could try to save it has a String:

Storing the array:

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("myByteArray", Arrays.toString(array));

Retrieving the array:

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String stringArray = settings.getString("myByteArray", null);

if (stringArray != null) {
    String[] split = stringArray.substring(1, stringArray.length()-1).split(", ");
    byte[] array = new byte[split.length];
    for (int i = 0; i < split.length; i++) {
      array[i] = Byte.parseByte(split[i]);
    }
}
like image 32
Francis Avatar answered Sep 18 '22 03:09

Francis