How to Save images to ImageView using Shared Preferences

陌路散爱 提交于 2019-11-29 17:57:15

Write Method to encode your Bitmap into string base64

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.d("Image Log:", imageEncoded);
    return imageEncoded;
}

Pass yourBitmap inside this method like something encodeTobase64 in your preference

        SharedPreferences myPrefrence = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = myPrefrence.edit();
        editor.putString("namePreferance", itemNAme);
        editor.putString("imagePreferance", encodeToBase64(yourBitmap));
        editor.commit();

And when you want any where display your image just convert it into Bitmap again using decodeToBase64 method

public static Bitmap decodeToBase64(String input) {
    byte[] decodedByte = Base64.decode(input, 0);
    return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}

here is code to get Bitmap again

SharedPreferences myPrefrence = getPreferences(MODE_PRIVATE);
String imageS = myPrefrence.getString("imagePreferance", "");
Bitmap imageB; 
if(!imageS.equals("")) imageB = decodeToBase64(imageS);

But in my opinion you should keep inside shared preference only path to bitmap.

Here is what I use :

public class AppPrefrances {
protected static AppPrefrances INSTANCE;
private static SharedPreferences prefs;

public static AppPrefrances getInstance(Context context) {
    if (INSTANCE == null) {
        INSTANCE = new AppPrefrances();
        prefs = PreferenceManager.getDefaultSharedPreferences(context);
    }

    return INSTANCE;
}

public void setPath(String path) {
    prefs.edit().putString("path", path).apply();
}

public String getPath() {
    return prefs.getString("path", "-1");
}
}

Now in your onActivityResult:

AppPrefrances.getInstance(getApplicationContext()).setPath(file_path);

Then in the onCreate, you check:

String check= AppPrefrances.getInstance(getApplicationContext()).setPath();
if(check!=null&&!check.equals("-1")){
imageView.setImageUri(Uri.parse(check));
}
#EliaszKubala,it my code.

here error: Unable to resume activity {com.example.thang.sdcardimagesactivity/com.example

 @Override
protected void onPause() {
    super.onPause();
    save();
}
public void save(){
    SharedPreferences sp = getSharedPreferences("AppSharedPref", 1); // Open SharedPreferences with name AppSharedPref
    SharedPreferences.Editor editor = sp.edit();
    editor.putString("ImagePath", "abc");
    editor.putString("ImagePath", encodeTobase64(bitmap)); // Store selectedImagePath with key "ImagePath". This key will be then used to retrieve data.
    editor.commit();
}

@Override
protected void onResume() {
    super.onResume();
    restore();
}
public void restore(){
    SharedPreferences sp = getSharedPreferences("AppSharedPref", 1);
    selectedImagePath = sp.getString("ImagePath", "abc");
    bitmap = decodeToBase64(selectedImagePath);
    imageView.setImageBitmap(bitmap);
}
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.d("Image Log:", imageEncoded);
    return imageEncoded;
}
public static Bitmap decodeToBase64(String input) {
    byte[] decodedByte = Base64.decode(input, 0);
    return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
  • #EliaszKubala.it my code.can u help me.:) – God_master Aug 5 '15 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!