Android Open Source - sdk_demo_app_android User Profile Storage






From Project

Back to project page sdk_demo_app_android.

License

The source code is released under:

By downloading or accessing this software, You agree to the Zendesk Terms of Service (https://www.zendesk.com/company/terms) and Application Developer and API License Agreement (https://www.zendesk.co...

If you think the Android project sdk_demo_app_android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.zendesk.rememberthedate.storage;
// www  .j  av a 2s  .c o  m
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;

import com.zendesk.rememberthedate.model.UserProfile;
import com.zendesk.sdk.util.StringUtils;

import java.io.ByteArrayOutputStream;

/**
 * Storage for user profile
 */
public class UserProfileStorage {

    private SharedPreferences mStorage;

    private static final String MY_DATES_STORE = "MyDates";
    private static final String IMAGE_DATA_KEY = "image_data";
    private static final String NAME_KEY = "name";
    private static final String EMAIL_KEY = "email";

    public UserProfileStorage(Context context) {
        mStorage = context.getSharedPreferences(MY_DATES_STORE, Context.MODE_PRIVATE);
    }


    public void storeUserProfile(String name, String email, Bitmap avatar) {

        String base64Avatar = null;

        if (avatar != null) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            avatar.compress(Bitmap.CompressFormat.PNG, 100, baos);
            byte[] b = baos.toByteArray();

            base64Avatar = Base64.encodeToString(b, Base64.DEFAULT);
        }

        mStorage.edit()
                .putString(NAME_KEY, name)
                .putString(EMAIL_KEY, email)
                .putString(IMAGE_DATA_KEY, base64Avatar)
                .apply();

    }

    public UserProfile getProfile() {

        UserProfile userProfile = new UserProfile();

        userProfile.setName(mStorage.getString(NAME_KEY, ""));
        userProfile.setEmail(mStorage.getString(EMAIL_KEY, ""));

        String base64Avatar = mStorage.getString(IMAGE_DATA_KEY, "");

        if (StringUtils.hasLength(base64Avatar)) {
            byte[] b = Base64.decode(base64Avatar, Base64.DEFAULT);
            Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
            userProfile.setAvatar(bitmap);
        }

        return userProfile;
    }
}




Java Source Code List

com.zendesk.rememberthedate.LocalNotification.java
com.zendesk.rememberthedate.model.UserProfile.java
com.zendesk.rememberthedate.rememberthedate.ApplicationTest.java
com.zendesk.rememberthedate.storage.UserProfileStorage.java
com.zendesk.rememberthedate.ui.CreateDateActivity.java
com.zendesk.rememberthedate.ui.CreateProfileActivity.java
com.zendesk.rememberthedate.ui.DateFragment.java
com.zendesk.rememberthedate.ui.HelpFragment.java
com.zendesk.rememberthedate.ui.MainActivity.java
com.zendesk.rememberthedate.ui.ProfileActivity.java
com.zendesk.rememberthedate.ui.RoundedImageView.java