0

I need to save my static image view to the shared preference, so that i can fit in the image to the HTML document. I even did try by encoding and decoding the Bitmap image using Base64.

public static String encodeToBase64(Bitmap image) {
    Bitmap bImage = image;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bImage.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] bytes = baos.toByteArray();
    String imageEncoded = Base64.encodeToString(bytes, Base64.DEFAULT);
    return imageEncoded;
}

public static Bitmap decodeBase64(String input) {
    byte[] decodeByte = Base64.decode(input, 0);
    return BitmapFactory.decodeByteArray(decodeByte, 0, decodeByte.length);
}
Birju Vachhani
  • 6,072
  • 4
  • 21
  • 43
Niroop Nife
  • 356
  • 1
  • 5
  • 18
  • you can save your image in asset folder – Priyanka Aug 02 '19 at 08:02
  • 1
    What have you tried so far? Can you put the code of encoding and decoding? – Skizo-ozᴉʞS ツ Aug 02 '19 at 08:06
  • the thing is i dont need to get the image in any particular activity, i just need it to be saved as a shared preference key so that i could retrieve it in the HTML code – Niroop Nife Aug 02 '19 at 08:08
  • ``` public static String encodeToBase64(Bitmap image){ Bitmap bImage = image; ByteArrayOutputStream baos = new ByteArrayOutputStream(); bImage.compress(Bitmap.CompressFormat.PNG, 100, baos); byte[] bytes = baos.toByteArray(); String imageEncoded = Base64.encodeToString(bytes, Base64.DEFAULT); return imageEncoded; } public static Bitmap decodeBase64(String input){ byte[] decodeByte = Base64.decode(input, 0); return BitmapFactory.decodeByteArray(decodeByte, 0, decodeByte.length); } ``` – Niroop Nife Aug 02 '19 at 08:08
  • 1
    Please add it to the question... and what error is giving to you when you using this code? Or what the actual result of using it? – Skizo-ozᴉʞS ツ Aug 02 '19 at 08:12
  • https://stackoverflow.com/questions/18072448/how-to-save-image-in-shared-preference-in-android-shared-preference-issue-in-a – Hardik Desai Aug 02 '19 at 08:17
  • Hello please check out this link for details https://stackoverflow.com/questions/18072448/how-to-save-image-in-shared-preference-in-android-shared-preference-issue-in-a – Hardik Desai Aug 02 '19 at 08:21

0 Answers0