Saving byte array using SharedPreferences

user2453055 picture user2453055 · Oct 24, 2013 · Viewed 22.3k times · Source

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 ?

Answer

Daniel Landau picture Daniel Landau · Feb 18, 2014

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);