Base64字符串图像未显示

时间:2018-05-06 13:23:52

标签: java android

我目前想在imageView中显示图像,然后通过共享首选项保存该图像。这一切都非常好。现在我的问题是,如果用户没有选择一个图像,我想要一个自定义的预定义图像背景。我将我的Image在线转换为base 64并使用此代码获得了我的String,就像所有其他设置一样。

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

就像我说的,如果我选择一张图片就可以正确保存。可能会使base64字符串格式错误吗?如果是这样我在哪里可以将图像转换为android base64格式?

我在这里转换了我的图像: Base 64 Encoder

如果您需要我的代码:

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

1 个答案:

答案 0 :(得分:0)

您使用过的在线转换器,为base64字符串添加前缀data:image/png;base64,,您需要将其删除。转换器实际上是生成数据URL而不是仅仅将图像的字节转换为base64字符串。