本文整理汇总了Java中android.util.Base64类的典型用法代码示例。如果您正苦于以下问题:Java Base64类的具体用法?Java Base64怎么用?Java Base64使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Base64类属于android.util包,在下文中一共展示了Base64类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: defaultsToMap
import android.util.Base64; //导入依赖的package包/类
private static Map<String, Object> defaultsToMap(JSONObject object) throws JSONException {
final Map<String, Object> map = new HashMap<String, Object>();
for (Iterator<String> keys = object.keys(); keys.hasNext(); ) {
String key = keys.next();
Object value = object.get(key);
if (value instanceof Integer) {
// setDefaults() takes Longs
value = Long.valueOf((Integer) value);
} else if (value instanceof JSONArray) {
JSONArray array = (JSONArray) value;
if (array.length() == 1 && array.get(0) instanceof String) {
//parse byte[] as Base64 String
value = Base64.decode(array.getString(0), Base64.DEFAULT);
} else {
//parse byte[] as numeric array
byte[] bytes = new byte[array.length()];
for (int i = 0; i < array.length(); i++)
bytes[i] = (byte) array.getInt(i);
value = bytes;
}
}
map.put(key, value);
}
return map;
}
示例2: getObject
import android.util.Base64; //导入依赖的package包/类
public Object getObject(String str) {
try {
Object string = getString(str);
if (TextUtils.isEmpty(string)) {
return null;
}
ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream
(Base64.decode(string, 2)));
string = objectInputStream.readObject();
objectInputStream.close();
return string;
} catch (Throwable th) {
Ln.w(th);
return null;
}
}
示例3: bmpToBase64
import android.util.Base64; //导入依赖的package包/类
/**
* 按照默认设置的压缩质量
* 将 Bitmap 转换为 Base64 照片字符串
*
* @param bmp
* @return
*/
public static final Observable<String> bmpToBase64(Bitmap bmp) {
int quality = 100;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) { // 4.0 及以下
quality = 50;
}
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN_MR2) { // 4.3 及以下
quality = 60;
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { // 5.0 以下
quality = 70;
}
return bmpToBase64(bmp, Bitmap.CompressFormat.JPEG, quality, Base64.NO_WRAP);
}
示例4: keys
import android.util.Base64; //导入依赖的package包/类
/**
* An aes key derived from a base64 encoded key. This does not generate the
* key. It's not random or a PBE key.
*
* @param keysStr a base64 encoded AES key / hmac key as base64(aesKey) : base64(hmacKey).
* @return an AES & HMAC key set suitable for other functions.
*/
public static SecretKeys keys(String keysStr) throws InvalidKeyException {
String[] keysArr = keysStr.split(":");
if (keysArr.length != 2) {
throw new IllegalArgumentException("Cannot parse aesKey:hmacKey");
} else {
byte[] confidentialityKey = Base64.decode(keysArr[0], BASE64_FLAGS);
if (confidentialityKey.length != AES_KEY_LENGTH_BITS /8) {
throw new InvalidKeyException("Base64 decoded key is not " + AES_KEY_LENGTH_BITS + " bytes");
}
byte[] integrityKey = Base64.decode(keysArr[1], BASE64_FLAGS);
if (integrityKey.length != HMAC_KEY_LENGTH_BITS /8) {
throw new InvalidKeyException("Base64 decoded key is not " + HMAC_KEY_LENGTH_BITS + " bytes");
}
return new SecretKeys(
new SecretKeySpec(confidentialityKey, 0, confidentialityKey.length, CIPHER),
new SecretKeySpec(integrityKey, HMAC_ALGORITHM));
}
}
示例5: encryptStr
import android.util.Base64; //导入依赖的package包/类
/**
* 加密数据
*
* @param str str
* @return 加密失败可能为null
*/
public static String encryptStr(String str) {
try {
PublicKey publicKey = getPublicKey(RSA_MODULUS,
RSA_PUBLIC_EXPONENT);
// 加解密类
Cipher cipher = Cipher.getInstance("RSA"); // "RSA/ECB/PKCS1Padding"
// 加密
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] dataEncode = cipher.doFinal(str.getBytes());
// 为了可读性,转为Base64,
byte[] tmp = Base64.encode(dataEncode, BASE64_FLAGS);
return new String(tmp);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例6: i
import android.util.Base64; //导入依赖的package包/类
private String i(String str) {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(str.getBytes());
OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
String str2 = null;
try {
GZIPOutputStream gZIPOutputStream = new GZIPOutputStream(byteArrayOutputStream);
byte[] bArr = new byte[1024];
while (true) {
int read = byteArrayInputStream.read(bArr, 0, 1024);
if (read == -1) {
break;
}
gZIPOutputStream.write(bArr, 0, read);
}
gZIPOutputStream.flush();
gZIPOutputStream.close();
byte[] toByteArray = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.flush();
byteArrayOutputStream.close();
byteArrayInputStream.close();
str2 = Base64.encodeToString(toByteArray, 2);
} catch (Throwable e) {
Ln.e(e);
}
return str2;
}
示例7: saveDeviceData
import android.util.Base64; //导入依赖的package包/类
/**
* 将对象储存到sharepreference
*
* @param key
* @param device
* @param <T>
*/
public static <T> boolean saveDeviceData(Context context, String key, T device) {
if (mSharedPreferences == null) {
mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try { //Device为自定义类
// 创建对象输出流,并封装字节流
ObjectOutputStream oos = new ObjectOutputStream(baos);
// 将对象写入字节流
oos.writeObject(device);
// 将字节流编码成base64的字符串
String oAuth_Base64 = new String(Base64.encode(baos
.toByteArray(), Base64.DEFAULT));
mSharedPreferences.edit().putString(key, oAuth_Base64).commit();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
示例8: textCompress
import android.util.Base64; //导入依赖的package包/类
public static String textCompress(String str) {
try {
Object array = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(str.length()).array();
OutputStream byteArrayOutputStream = new ByteArrayOutputStream(str.length());
GZIPOutputStream gZIPOutputStream = new GZIPOutputStream(byteArrayOutputStream);
gZIPOutputStream.write(str.getBytes("UTF-8"));
gZIPOutputStream.close();
byteArrayOutputStream.close();
Object obj = new byte[(byteArrayOutputStream.toByteArray().length + 4)];
System.arraycopy(array, 0, obj, 0, 4);
System.arraycopy(byteArrayOutputStream.toByteArray(), 0, obj, 4, byteArrayOutputStream.toByteArray().length);
return Base64.encodeToString(obj, 8);
} catch (Exception e) {
return "";
}
}
示例9: HttpPeerConnector
import android.util.Base64; //导入依赖的package包/类
public HttpPeerConnector(String peerUrl, SiteToSiteClientConfig siteToSiteClientConfig, SiteToSiteRemoteCluster siteToSiteRemoteCluster) {
this.peerUrl = peerUrl;
this.siteToSiteClientConfig = siteToSiteClientConfig;
this.siteToSiteRemoteCluster = siteToSiteRemoteCluster;
SSLContext sslContext = siteToSiteRemoteCluster.getSslContext();
if (sslContext != null) {
socketFactory = sslContext.getSocketFactory();
} else {
socketFactory = null;
}
proxy = getProxy(siteToSiteRemoteCluster);
String proxyUsername = siteToSiteRemoteCluster.getProxyUsername();
if (proxy != null && proxyUsername != null && !proxyUsername.isEmpty()) {
proxyAuth = siteToSiteRemoteCluster.getProxyAuthorizationType() + " " + Base64.encodeToString((proxyUsername + ":" + siteToSiteRemoteCluster.getProxyPassword()).getBytes(Charsets.ISO_8859_1), Base64.DEFAULT);
} else {
proxyAuth = null;
}
}
示例10: processIniciatorState1
import android.util.Base64; //导入依赖的package包/类
private void processIniciatorState1(int accountId, int peerId, @NonNull KeyExchangeSession session, @NonNull ExchangeMessage message) {
String hisAesKey = message.getAesKey();
PrivateKey myPrivateKey = session.getMyPrivateKey();
try {
byte[] hisAesEncoded = Base64.decode(hisAesKey, Base64.DEFAULT);
String hisOriginalAes = CryptHelper.decryptRsa(hisAesEncoded, myPrivateKey);
session.setHisAesKey(hisOriginalAes);
String myOriginalAesKey = CryptHelper.generateRandomAesKey(Version.ofCurrent().getAesKeySize());
session.setMyAesKey(myOriginalAesKey);
PublicKey hisPublicKey = CryptHelper.createRsaPublicKeyFromString(message.getPublicKey());
byte[] myEncodedAesKey = CryptHelper.encryptRsa(myOriginalAesKey, hisPublicKey);
String myEncodedAesKeyBase64 = Base64.encodeToString(myEncodedAesKey, Base64.DEFAULT);
Logger.d(TAG, "processIniciatorState1, myOriginalAesKey: " + myOriginalAesKey + ", hisOriginalAes: " + hisOriginalAes);
ExchangeMessage m = new ExchangeMessage.Builder(Version.CURRENT, session.getId(), SessionState.INITIATOR_STATE_2)
.setAesKey(myEncodedAesKeyBase64)
.create();
sendMessage(accountId, peerId, m);
} catch (InvalidKeyException | BadPaddingException | IllegalBlockSizeException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeySpecException e) {
e.printStackTrace();
}
}
示例11: decode
import android.util.Base64; //导入依赖的package包/类
/**
* Decode the user's email address from Base64
*
* @param accountList the {@link SaiyAccountList} object
* @return the updated {@link SaiyAccountList}
*/
private static SaiyAccountList decode(@NonNull final SaiyAccountList accountList) {
try {
for (final SaiyAccount account : accountList.getSaiyAccountList()) {
account.setAccountName(new String(Base64.decode(account.getAccountName(), Base64.NO_WRAP),
Constants.ENCODING_UTF8));
}
} catch (final UnsupportedEncodingException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "encode UnsupportedEncodingException");
e.printStackTrace();
}
}
return accountList;
}
示例12: getStringImage
import android.util.Base64; //导入依赖的package包/类
public String getStringImage(Bitmap bmp){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
示例13: getImage
import android.util.Base64; //导入依赖的package包/类
void getImage() //profile pic
{
ImageRequest request = new ImageRequest("http://ec2-52-14-50-89.us-east-2.compute.amazonaws.com/static/userdata/"+name+"/thumb.png", ///"+email+" in btw userdata/ /thumb.png
new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap bitmap) {
pro=bitmap;
ByteArrayOutputStream baos=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
byte [] b=baos.toByteArray();
String temp= Base64.encodeToString(b, Base64.DEFAULT);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("profile_pic",temp);
editor.commit();
Log.e("mytag","Saved propic"+pro);
//count++;
}
}, 0, 0, null,
new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
// mImageView.setImageResource(R.drawable.image_load_error);
Log.e("Home_Acitivity","No img found");
//count++;
}
});
// MySingleton.getMyInstance(getApplicationContext()).addToReqQue(request);
RequestQueue queue= Volley.newRequestQueue(getApplicationContext());
queue.add(request);
}
示例14: decrypt
import android.util.Base64; //导入依赖的package包/类
public static String decrypt( String password, String encryptedData ) throws Exception
{
byte[] secretKey = generateKey( password.getBytes() );
SecretKeySpec secretKeySpec = new SecretKeySpec( secretKey, CIPHER_ALGORITHM );
Cipher cipher = Cipher.getInstance( CIPHER_ALGORITHM );
cipher.init( Cipher.DECRYPT_MODE, secretKeySpec );
byte[] encrypted = Base64.decode( encryptedData, Base64.DEFAULT );
byte[] decrypted = cipher.doFinal( encrypted );
return new String( decrypted );
}
示例15: setHTML
import android.util.Base64; //导入依赖的package包/类
public void setHTML(byte bytes[])
{
// preserve zoom level between pages
int zoom = (int)(100 * mScale);
String b64 = Base64.encodeToString(bytes, Base64.DEFAULT);
loadData(b64, "text/html; charset=utf-8", "base64");
setInitialScale(zoom);
scrollTo(0, 0);
}