Example usage for android.util Base64 decode

List of usage examples for android.util Base64 decode

Introduction

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

Prototype

public static byte[] decode(byte[] input, int flags) 

Source Link

Document

Decode the Base64-encoded data in input and return the data in a new byte array.

Usage

From source file:com.yanzhenjie.nohttp.cache.CacheEntity.java

/**
 * @param data the data to set.
 */
public void setDataBase64(String data) {
    this.data = Base64.decode(data, Base64.DEFAULT);
}

From source file:fr.outadev.skinswitch.MojangAccountSkin.java

@Override
public void downloadSkinFromSource(Context context) throws HttpRequest.HttpRequestException, IOException {
    if (getUuid() == null) {
        return;//from  ww  w.  j a v  a 2  s  .  co m
    }

    //we have to request a file on Mojang's server if we wanna retrieve the player's skin from his UUID.
    String body = HttpRequest.get(SESSION_API_URL + getUuid()).useCaches(false).body();

    if (body == null) {
        throw new HttpRequest.HttpRequestException(new IOException("Response to get the skin URL was empty."));
    }

    //we're expecting a JSON document that looks like this:
    /*
       {
     ...
     "properties": [
         {
             "name": "textures",
             "value": "<base64 string>"
         }
     ]
       }
     */

    try {
        //trying to reach the properties.value string
        JSONObject obj = new JSONObject(body);
        JSONArray properties = obj.getJSONArray("properties");

        if (properties != null) {
            for (int i = 0; i < properties.length(); i++) {
                if (properties.getJSONObject(i).getString("name").equals("textures")) {
                    //once that string is reached, we have to decode it: it's in base64
                    String base64info = properties.getJSONObject(i).getString("value");
                    JSONObject textureInfo = new JSONObject(
                            new String(Base64.decode(base64info, Base64.DEFAULT)));

                    //the decoded string is also a JSON document, so we parse that. should look like this:
                    /*
                       {
                           ...
                           "textures": {
                     "SKIN": {
                         "url": "<player skin URL>"
                     },
                     ...
                           }
                       }
                     */

                    //we want to retrieve the textures.SKIN.url string. that's the skin's URL. /FINALLY/.
                    String url = textureInfo.getJSONObject("textures").getJSONObject("SKIN").getString("url");

                    if (url != null) {
                        //download the skin from the provided URL
                        byte[] response = HttpRequest.get(url).useCaches(true).bytes();

                        if (response == null) {
                            throw new HttpRequest.HttpRequestException(
                                    new IOException("Couldn't download " + this));
                        }

                        //decode the bitmap and store it
                        Bitmap bmp = BitmapFactory.decodeByteArray(response, 0, response.length);

                        if (bmp != null) {
                            deleteAllCacheFilesFromFilesystem(context);
                            saveRawSkinBitmap(context, bmp);
                            bmp.recycle();
                        }
                    }
                }
            }
        }
    } catch (JSONException e) {
        throw new HttpRequest.HttpRequestException(
                new IOException("The response from Mojang was invalid. Woops."));
    }
}

From source file:com.amanmehara.programming.android.adapters.DetailAdapter.java

private String decodeContent(String content) {
    return new String(Base64.decode(content, Base64.DEFAULT));
}

From source file:edu.stanford.mobisocial.dungbeetle.obj.action.PlayAllAudioAction.java

public void playNextSong() {
    if (c.isAfterLast()) {
        c.close();/*ww  w .  j  a  v a 2  s.  co m*/
        alert.dismiss();
        return;
    }

    try {
        final JSONObject objData = new JSONObject(c.getString(c.getColumnIndex(DbObject.JSON)));
        Runnable r = new Runnable() {
            @Override
            public void run() {

                Log.w("PlayAllAudioAction", objData.optString("feedName"));
                byte bytes[] = Base64.decode(objData.optString(VoiceObj.DATA), Base64.DEFAULT);

                File file = new File(getTempFilename());
                try {
                    OutputStream os = new FileOutputStream(file);
                    BufferedOutputStream bos = new BufferedOutputStream(os);
                    bos.write(bytes, 0, bytes.length);
                    bos.flush();
                    bos.close();

                    copyWaveFile(getTempFilename(), getFilename());
                    deleteTempFile();

                    mp = new MediaPlayer();

                    mp.setDataSource(getFilename());
                    mp.setOnCompletionListener(new OnCompletionListener() {

                        public void onCompletion(MediaPlayer m) {
                            Log.w("PlayAllAudioAction", "finished");
                            c.moveToNext();
                            playNextSong();
                        }
                    });
                    mp.prepare();
                    mp.start();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        if (context instanceof Activity) {
            ((Activity) context).runOnUiThread(r);
        } else {
            r.run();
        }
    } catch (Exception e) {
    }

}

From source file:com.kii.sample.balance.samples.SampleLoginDialogFragment.java

@OnClick(R.id.button_submit)
void submitClicked() {

    // gets username / password
    String username = mUsernameEdit.getText().toString();
    String password = mPasswordEdit.getText().toString();

    SharedPreferences prefs = getActivity().getSharedPreferences("data", Context.MODE_PRIVATE);

    if (!prefs.contains(username)) {
        showErrorMessage(MESSAGE_INVALID_USERNAME);
        return;//from  ww w.  j a  va  2  s. c o  m
    }
    RAWPASS = prefs.getString(username, "");
    rawDecryptedToken = Base64.decode(RAWPASS, Base64.DEFAULT);

    FacebookConcealSample fc = new FacebookConcealSample();
    PASS = fc.decryption(rawDecryptedToken, getActivity());

    if (!PASS.equals(password)) {
        showErrorMessage(MESSAGE_INVALID_PASSWORD);
        return;
    }

    // show progress
    ProgressDialogFragment progress = ProgressDialogFragment.newInstance(getActivity(), R.string.login,
            R.string.login);
    progress.show(getFragmentManager(), ProgressDialogFragment.FRAGMENT_TAG);

    Fragment target = getTargetFragment();
    target.onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, null);
    dismiss();

    // call user login API
    //        KiiUser.logIn(new KiiUserCallBack() {
    //            @Override
    //            public void onLoginCompleted(int token, KiiUser user, Exception e) {
    //                super.onLoginCompleted(token, user, e);
    //
    //                ProgressDialogFragment.hide(getFragmentManager());
    //                if (e != null) {
    //                    showErrorMessage(MESSAGE_LOGIN_FAILED);
    //                    return;
    //                }
    //
    //                // notify caller fragment that registration is done.
    //                Fragment target = getTargetFragment();
    //                if (target == null) {
    //                    dismiss();
    //                    return;
    //                }
    //                target.onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, null);
    //                dismiss();
    //            }
    //        }, username, password);
}

From source file:com.hybris.mobile.lib.commerce.helper.SecurityHelper.java

/**
 * Decrypt secure String associated to the key
 *
 * @param value The value to decrypt//w  w  w  .j a  v  a 2 s.c om
 * @return decrypted string
 */
public static String decrypt(String value) {
    String decryptedText = "";
    try {
        if (StringUtils.isNotBlank(value)) {
            Cipher cipher = Cipher.getInstance(CIPHER);
            cipher.init(Cipher.DECRYPT_MODE, mSecretKeySpec, mIvParameterSpec);
            decryptedText = new String(cipher.doFinal(Base64.decode(value, Base64.NO_CLOSE)), ENCODING);
            return decryptedText;
        }
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "Algorithm not found.");
    } catch (NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
        Log.e(TAG, "Exception during decrypt");
    } catch (InvalidKeyException e) {
        Log.e(TAG, "No valid key provided.");
    } catch (InvalidAlgorithmParameterException e) {
        Log.e(TAG, "Algorithm parameter specification is invalid");
    } catch (UnsupportedEncodingException e) {
        Log.e(TAG, "Character to convert is unavailable");
    }

    return decryptedText;
}

From source file:com.achep.base.billing.CheckoutInternal.java

/**
 * Method deciphers previously ciphered message
 *
 * @param message ciphered message/*from w w  w  . java2  s  . co m*/
 * @param salt    salt which was used for ciphering
 * @return deciphered message
 */
@NonNull
private String fromX(@NonNull String message, @NonNull String salt) throws IllegalArgumentException {
    return x(new String(Base64.decode(message, Base64.URL_SAFE)), salt);
}

From source file:de.aw.monma.hbci.FragmentMasterPassword.java

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    passwordView = view.findViewById(R.id.etPassword);
    passwordView.setOnEditorActionListener(new EditText.OnEditorActionListener() {
        @Override/*from   www .ja  v  a2 s .c o  m*/
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                onClick(passwordView);
                return true;
            }
            return false;
        }
    });
    newPasswordView = view.findViewById(R.id.etNewPassword);
    AWApplication.Log("Passwort Abfrage gestartet");
    String key = prefs.getString(getString(R.string.key), null);
    if (key != null) {
        mKey = Base64.decode(key, Base64.NO_WRAP);
    }
    if (mKey != null) {
        newPasswordView.setVisibility(View.GONE);
    }
    view.findViewById(R.id.loginBtn).setOnClickListener(this);
}

From source file:com.pimp.companionforband.fragments.theme.ThemeFragment.java

@Override
public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    imageView = (ImageView) view.findViewById(R.id.selected_me_tile_image_view);

    SharedPreferences settings = getActivity().getSharedPreferences("MyPrefs", 0);
    Drawable meTileDrawable = null;/*from  w ww. jav  a  2 s .co  m*/
    String encoded = settings.getString("me_tile_image", "null");
    if (!encoded.equals("null")) {
        byte[] imageAsBytes = Base64.decode(encoded.getBytes(), Base64.DEFAULT);
        meTileDrawable = new BitmapDrawable(getResources(),
                BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
    }
    if (meTileDrawable != null)
        imageView.setImageDrawable(meTileDrawable);

    Button btnUpdateMe, btnPickMe, btnUpdateTheme, btnGetMeTile, btnGetTheme, btnGetColors;
    btnPickMe = (Button) view.findViewById(R.id.pick_me_tile_button);
    btnPickMe.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType("image/*");
            startActivityForResult(intent, 9);
        }
    });

    btnGetMeTile = (Button) view.findViewById(R.id.get_me_tile_button);
    btnGetMeTile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new getMeTask().execute();
        }
    });

    btnUpdateMe = (Button) view.findViewById(R.id.update_me_tile_button);
    btnUpdateMe.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
            new appTask().execute();
        }
    });

    SharedPreferences sharedPreferences = getContext().getSharedPreferences("MyPrefs", 0);

    base = sharedPreferences.getInt("base", -16777216);
    highlight = sharedPreferences.getInt("highLight", -16777216);
    lowlight = sharedPreferences.getInt("lowLight", -16777216);
    secondaryText = sharedPreferences.getInt("secondaryText", -16777216);
    highContrast = sharedPreferences.getInt("highContrast", -16777216);
    muted = sharedPreferences.getInt("muted", -16777216);

    btnGetTheme = (Button) view.findViewById(R.id.get_theme_button);
    btnGetTheme.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new getThemeTask().execute(view);
        }
    });

    btnUpdateTheme = (Button) view.findViewById(R.id.update_theme_button);
    btnUpdateTheme.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new themeTask().execute();
        }
    });

    btnBase = (Button) view.findViewById(R.id.base);
    btnHighlight = (Button) view.findViewById(R.id.highlight);
    btnLowlight = (Button) view.findViewById(R.id.lowlight);
    btnSecondaryText = (Button) view.findViewById(R.id.secondaryText);
    btnHighContrast = (Button) view.findViewById(R.id.highContrast);
    btnMuted = (Button) view.findViewById(R.id.muted);

    btnBase.setBackgroundColor(Color.parseColor(String.format("#%06X", (0xFFFFFF & base))));
    btnHighlight.setBackgroundColor(Color.parseColor(String.format("#%06X", (0xFFFFFF & highlight))));
    btnLowlight.setBackgroundColor(Color.parseColor(String.format("#%06X", (0xFFFFFF & lowlight))));
    btnSecondaryText.setBackgroundColor(Color.parseColor(String.format("#%06X", (0xFFFFFF & secondaryText))));
    btnHighContrast.setBackgroundColor(Color.parseColor(String.format("#%06X", (0xFFFFFF & highContrast))));
    btnMuted.setBackgroundColor(Color.parseColor(String.format("#%06X", (0xFFFFFF & muted))));

    btnGetColors = (Button) view.findViewById(R.id.get_colors_button);
    btnGetColors.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
            if (bitmapDrawable.getBitmap() != null && !bitmapDrawable.getBitmap().isRecycled()) {
                Palette.from(bitmapDrawable.getBitmap()).generate(paletteListener);
            }
        }
    });
}

From source file:ca.hoogit.garagepi.Camera.CameraFragment.java

private void handleUpdatingCameraFeed(String frame) {
    if (isAdded()) {
        Log.v(TAG, "handleUpdatingCameraFeed: Updating image view");
        byte[] decoded = Base64.decode(frame, Base64.DEFAULT);
        Bitmap bitmap = BitmapFactory.decodeByteArray(decoded, 0, decoded.length);
        Drawable drawable = new BitmapDrawable(getResources(), bitmap);
        mCameraFeed.setImageDrawable(drawable);
    }// w  w w .  j  a v a  2 s.co m
}