Example usage for android.util Base64 encodeToString

List of usage examples for android.util Base64 encodeToString

Introduction

In this page you can find the example usage for android.util Base64 encodeToString.

Prototype

public static String encodeToString(byte[] input, int offset, int len, int flags) 

Source Link

Document

Base64-encode the given data and return a newly allocated String with the result.

Usage

From source file:Main.java

public static String base64UrlEncode(byte[] arg) {
    return Base64.encodeToString(arg, 0, arg.length, Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING);
}

From source file:Main.java

public static String bitmapToBase64(Bitmap bitmap) {

    try {/*from  w w w .java 2 s .co  m*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        baos.close();
        byte[] buffer = baos.toByteArray();
        String photo = Base64.encodeToString(buffer, 0, buffer.length, Base64.DEFAULT);
        return photo;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

@SuppressLint("NewApi")
public static String getBitmapStrBase64(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 100, baos);
    byte[] bytes = baos.toByteArray();
    String data = Base64.encodeToString(bytes, 0, bytes.length, Base64.DEFAULT);

    if (bitmap != null) {
        bitmap.recycle();/*w w  w.  j av  a2s.  c  o m*/
        bitmap = null;
    }

    return data;
}

From source file:cc.flydev.launcher.InstallShortcutReceiver.java

private static void addToInstallQueue(SharedPreferences sharedPrefs, PendingInstallShortcutInfo info) {
    synchronized (sLock) {
        try {/*  www  .  jav  a 2s.  com*/
            JSONStringer json = new JSONStringer().object().key(DATA_INTENT_KEY).value(info.data.toUri(0))
                    .key(LAUNCH_INTENT_KEY).value(info.launchIntent.toUri(0)).key(NAME_KEY).value(info.name);
            if (info.icon != null) {
                byte[] iconByteArray = ItemInfo.flattenBitmap(info.icon);
                json = json.key(ICON_KEY)
                        .value(Base64.encodeToString(iconByteArray, 0, iconByteArray.length, Base64.DEFAULT));
            }
            if (info.iconResource != null) {
                json = json.key(ICON_RESOURCE_NAME_KEY).value(info.iconResource.resourceName);
                json = json.key(ICON_RESOURCE_PACKAGE_NAME_KEY).value(info.iconResource.packageName);
            }
            json = json.endObject();
            SharedPreferences.Editor editor = sharedPrefs.edit();
            if (DBG)
                Log.d(TAG, "Adding to APPS_PENDING_INSTALL: " + json);
            addToStringSet(sharedPrefs, editor, APPS_PENDING_INSTALL, json.toString());
            editor.commit();
        } catch (org.json.JSONException e) {
            Log.d(TAG, "Exception when adding shortcut: " + e);
        }
    }
}

From source file:com.andernity.launcher2.InstallShortcutReceiver.java

private static void addToInstallQueue(SharedPreferences sharedPrefs, PendingInstallShortcutInfo info) {
    synchronized (sLock) {
        try {/*from w  w w  . j a  va  2s. c  om*/
            JSONStringer json = new JSONStringer().object().key(DATA_INTENT_KEY).value(info.data.toUri(0))
                    .key(LAUNCH_INTENT_KEY).value(info.launchIntent.toUri(0)).key(NAME_KEY).value(info.name);
            if (info.icon != null) {
                byte[] iconByteArray = ItemInfo.flattenBitmap(info.icon);
                json = json.key(ICON_KEY)
                        .value(Base64.encodeToString(iconByteArray, 0, iconByteArray.length, Base64.DEFAULT));
            }
            if (info.iconResource != null) {
                json = json.key(ICON_RESOURCE_NAME_KEY).value(info.iconResource.resourceName);
                json = json.key(ICON_RESOURCE_PACKAGE_NAME_KEY).value(info.iconResource.packageName);
            }
            json = json.endObject();
            SharedPreferences.Editor editor = sharedPrefs.edit();
            addToStringSet(sharedPrefs, editor, APPS_PENDING_INSTALL, json.toString());
            editor.commit();
        } catch (org.json.JSONException e) {
            Log.d("InstallShortcutReceiver", "Exception when adding shortcut: " + e);
        }
    }
}

From source file:com.aidy.launcher3.ui.receiver.InstallShortcutReceiver.java

private static void addToInstallQueue(SharedPreferences sharedPrefs, PendingInstallShortcutInfo info) {
    synchronized (sLock) {
        try {//from   www .  j  a  v  a2s  .  c  o  m
            JSONStringer json = new JSONStringer().object().key(DATA_INTENT_KEY).value(info.data.toUri(0))
                    .key(LAUNCH_INTENT_KEY).value(info.launchIntent.toUri(0)).key(NAME_KEY).value(info.name);
            if (info.icon != null) {
                byte[] iconByteArray = ItemInfoBean.flattenBitmap(info.icon);
                json = json.key(ICON_KEY)
                        .value(Base64.encodeToString(iconByteArray, 0, iconByteArray.length, Base64.DEFAULT));
            }
            if (info.iconResource != null) {
                json = json.key(ICON_RESOURCE_NAME_KEY).value(info.iconResource.resourceName);
                json = json.key(ICON_RESOURCE_PACKAGE_NAME_KEY).value(info.iconResource.packageName);
            }
            json = json.endObject();
            SharedPreferences.Editor editor = sharedPrefs.edit();
            if (DBG)
                Log.d(TAG, "Adding to APPS_PENDING_INSTALL: " + json);
            addToStringSet(sharedPrefs, editor, APPS_PENDING_INSTALL, json.toString());
            editor.commit();
        } catch (org.json.JSONException e) {
            Log.d(TAG, "Exception when adding shortcut: " + e);
        }
    }
}

From source file:com.grarak.kerneladiutor.utils.Utils.java

public static long computeSHAHash(String password) throws Exception {
    long begin = System.nanoTime();
    MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
    messageDigest.update(password.getBytes("ASCII"));
    byte[] data = messageDigest.digest();
    Base64.encodeToString(data, 0, data.length, 0);
    return System.nanoTime() - begin;
}

From source file:com.hmsoft.libcommon.gopro.GoProController.java

private void logCommandAndResponse(String url, byte[] response) {
    String responseStr = Base64.encodeToString(response, 0, response.length > 512 ? 512 : response.length,
            Base64.NO_WRAP | Base64.URL_SAFE);

    Logger.info(TAG, removePassword(url) + " = " + responseStr);
}

From source file:com.ledger.android.u2f.bridge.MainActivity.java

private String createU2FResponseRegister(U2FContext context, byte[] registerResponse) {
    try {/*  ww  w  .j  ava2 s.  c om*/
        JSONObject response = new JSONObject();
        response.put(TAG_JSON_TYPE, REGISTER_RESPONSE_TYPE);
        response.put(TAG_JSON_REQUESTID, context.getRequestId());
        JSONObject responseData = new JSONObject();
        responseData.put(TAG_JSON_REGISTRATIONDATA, Base64.encodeToString(registerResponse, 0,
                registerResponse.length - 2, Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING));
        responseData.put(TAG_JSON_VERSION, VERSION_U2F_V2);
        String clientData = createClientData(context);
        responseData.put(TAG_JSON_CLIENTDATA, Base64.encodeToString(clientData.getBytes("UTF-8"),
                Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING));
        response.put(TAG_JSON_RESPONSEDATA, responseData);
        return response.toString();
    } catch (Exception e) {
        Log.e(TAG, "Error encoding request");
        return null;
    }
}

From source file:hochschuledarmstadt.photostream_tools.PhotoStreamClientImpl.java

private JSONObject createJsonObject(byte[] imageBytes, String comment) throws JSONException {
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("image", Base64.encodeToString(imageBytes, 0, imageBytes.length, Base64.DEFAULT));
    jsonObject.put("comment", comment);
    return jsonObject;
}