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