0

I currently want to show a image in an imageView and then save that image via shared preferences. This works all very good. Now my Problem is that I want a custom predefined image background if the user hasnt choose one image. I converted my Image online to base 64 and got my String like all other settings with this code.

pic.setImageBitmap(decodeToBitmapAndGet(sharedPreferences.getString(String.valueOf(R.string.user_picture_key), String.valueOf(R.string.standard_profile_picture))));

Like I said if I choose a picture is saves correctly. May the base64 string be malformed? If so where can I convert an image to android base64 format?

I converted my image here: Base 64 Encoder

If you need my Code:

public void updateValuesFromPreferences(final Activity activity, View layout){
    TextView user = layout.findViewById(R.id.current_username);
    TextView email = layout.findViewById(R.id.current_email);
    CircleImageView pic = layout.findViewById(R.id.current_profile_picture);
    ImageView bg = layout.findViewById(R.id.current_profile_background);
    pic.setImageBitmap(decodeToBitmapAndGet(sharedPreferences.getString(String.valueOf(R.string.user_picture_key), String.valueOf(R.string.standard_profile_picture))));
    pic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            checkForPermissionAndAskIfNotGranted(activity);
            Intent getPhoto = new Intent(Intent.ACTION_PICK);
            getPhoto.setType("image/*");
            activity.startActivityForResult(getPhoto, Config.RESULT_USER_PROFILE_PICTURE);
        }
    });
    bg.setImageBitmap(decodeToBitmapAndGet(sharedPreferences.getString(String.valueOf(R.string.user_background_key), String.valueOf(R.string.standard_background_picture))));
    bg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            checkForPermissionAndAskIfNotGranted(activity);
            Intent getPhoto = new Intent(Intent.ACTION_PICK);
            getPhoto.setType("image/*");
            activity.startActivityForResult(getPhoto, Config.RESULT_USER_BACKGROUND);
        }
    });
    String currUsername = sharedPreferences.getString(String.valueOf(R.string.user_username_key), "Username");
    String currEmail = sharedPreferences.getString(String.valueOf(R.string.user_email_key), "EMail");

    user.setText(currUsername);
    email.setText(currEmail);

    Log.i(Config.TAG, currUsername + currEmail);
}
public void encodeToBase64AndSave(Bitmap image, int requestCode){
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
    byte[] bytes = outputStream.toByteArray();
    String base64 = Base64.encodeToString(bytes, Base64.DEFAULT);
    switch (requestCode){
        case Config.RESULT_USER_PROFILE_PICTURE:
            editor.putString(String.valueOf(R.string.user_picture_key), base64);
            break;
        default:
            editor.putString(String.valueOf(R.string.user_background_key), base64);
            break;
    }
    editor.apply();
}

public Bitmap decodeToBitmapAndGet(String base64){
    byte[] bytes = Base64.decode(base64, 0);
    Log.i(Config.TAG, base64);
    return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Samuel M.
  • 5
  • 3

1 Answers1

0

The online convertor you've used, adds a prefix to the base64 String something like data:image/png;base64,, you need to remove that. The convertor is actually generating data URLs instead of just converting the image's bytes to a base64 string.

Titus
  • 22,031
  • 1
  • 23
  • 33
  • I tested it without that line but still only a white background. If you want to check my String it is here: https://justpaste.it/5ln7u – Samuel M. May 06 '18 at 13:55
  • @SamuelM. The Java encoder/decoder is probably using `-` instead of `+` and `_` instead of `/`. – Titus May 06 '18 at 14:05
  • @SamuelM. Try this string https://justpaste.it/5wck4 I've replaced the characters I've mentioned in my previous comment. It may not work, there are a couple of different base64 variants, this is just one of them. – Titus May 06 '18 at 14:08
  • Thanks. But now I found another issue. If I want to give the String of the sharedpreferences it gives me the standard value which is ok. But the standard value was a number (I think the id of the string in the xml). So it only shows me the String I want if I put it inline. I also tried yours. cleared my appdata but still nothing :/ – Samuel M. May 06 '18 at 14:29
  • @SamuelM. Oh, I see the problem now, you need to take a look at [this](https://stackoverflow.com/a/7493367/1552587) it contains an example of how to retrieve a string resources. You can find more details in the [documentation](https://developer.android.com/guide/topics/resources/string-resource) – Titus May 06 '18 at 14:33
  • I think I have to do it the other way. I am going to store the image in my drawable Folder and convert it programmatically via the same method I generated to convert the userselected images. – Samuel M. May 06 '18 at 14:33
  • @SamuelM. No need for that, you just need to use `getString(R.string.standard_background_picture)` to get the string resource, `R.string.standard_background_picture` is just the ID of that resource (an `int` variable). – Titus May 06 '18 at 14:35
  • Thank you so much. It worked. As I was in the Application class I had to put my activity before .getResources(). But Now it works very well. Thanks again – Samuel M. May 06 '18 at 14:40