下面列出了android.util.Base64#encode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected String encodeOpenWithIntent(Intent owpIntent) {
/*
* Encode OpenWith intent
* Real 3rd party apps should never run this function as DropboxApp does this entirely
*/
Bundle extras = owpIntent.getExtras();
extras.putString("_action", owpIntent.getAction());
extras.putParcelable("_uri", owpIntent.getData());
extras.putString("_type", owpIntent.getType());
// marshall it!
final Parcel parcel = Parcel.obtain();
parcel.writeBundle(extras);
byte[] b = parcel.marshall();
parcel.recycle();
return new String(Base64.encode(b, 0));
}
/**
* Compress bitmap using jpeg, convert to Base64 encoded string, and return to JavaScript.
*
* @param bitmap
*/
public void processPicture(Bitmap bitmap) {
ByteArrayOutputStream jpeg_data = new ByteArrayOutputStream();
try {
if (bitmap.compress(CompressFormat.JPEG, mQuality, jpeg_data)) {
byte[] code = jpeg_data.toByteArray();
byte[] output = Base64.encode(code, Base64.DEFAULT);
String js_out = new String(output);
this.callbackContext.success(js_out);
js_out = null;
output = null;
code = null;
}
} catch (Exception e) {
this.failPicture("Error compressing image.");
}
jpeg_data = null;
}
/**
* Returns an X-Geo HTTP header string if:
* 1. The current mode is not incognito.
* 2. The url is a google search URL (e.g. www.google.co.uk/search?q=cars), and
* 3. The user has not disabled sharing location with this url, and
* 4. There is a valid and recent location available.
*
* Returns null otherwise.
*
* @param context The Context used to get the device location.
* @param url The URL of the request with which this header will be sent.
* @param isIncognito Whether the request will happen in an incognito tab.
* @return The X-Geo header string or null.
*/
public static String getGeoHeader(Context context, String url, boolean isIncognito) {
if (!isGeoHeaderEnabledForUrl(context, url, isIncognito, true)) {
return null;
}
// Only send X-Geo header if there's a fresh location available.
Location location = GeolocationTracker.getLastKnownLocation(context);
if (location == null) {
recordHistogram(UMA_LOCATION_NOT_AVAILABLE);
return null;
}
if (GeolocationTracker.getLocationAge(location) > MAX_LOCATION_AGE) {
recordHistogram(UMA_LOCATION_STALE);
return null;
}
recordHistogram(UMA_HEADER_SENT);
// Timestamp in microseconds since the UNIX epoch.
long timestamp = location.getTime() * 1000;
// Latitude times 1e7.
int latitude = (int) (location.getLatitude() * 10000000);
// Longitude times 1e7.
int longitude = (int) (location.getLongitude() * 10000000);
// Radius of 68% accuracy in mm.
int radius = (int) (location.getAccuracy() * 1000);
// Encode location using ascii protobuf format followed by base64 encoding.
// https://goto.google.com/partner_location_proto
String locationAscii = String.format(Locale.US,
"role:1 producer:12 timestamp:%d latlng{latitude_e7:%d longitude_e7:%d} radius:%d",
timestamp, latitude, longitude, radius);
String locationBase64 = new String(Base64.encode(locationAscii.getBytes(), Base64.NO_WRAP));
return "X-Geo: a " + locationBase64;
}
PasswordNetworkManager(@NonNull ShaarliAccount account) {
this.mShaarliUrl = account.getUrlShaarli();
this.mUsername = account.getUsername();
this.mPassword = account.getPassword();
this.mValidateCert = account.isValidateCert();
if (!"".equals(account.getBasicAuthUsername()) && !"".equals(account.getBasicAuthPassword())) {
String login = account.getBasicAuthUsername() + ":" + account.getBasicAuthPassword();
this.mBasicAuth = new String(Base64.encode(login.getBytes(), Base64.NO_WRAP));
} else {
this.mBasicAuth = "";
}
}
/**
* Decrypt a base64 encoded, DES encrypted string and return
* the unencrypted string.
* @param encryptedString The base64 encoded string to decrypt.
* @return String The decrypted string.
* @throws Exception If an error occurs.
*/
public String decryptBase64 (String encryptedString) throws Exception {
// Encode bytes to base64 to get a string
byte [] decodedBytes = Base64.encode(encryptedString.getBytes(), Base64.DEFAULT);
// Decrypt
byte[] unencryptedByteArray = decryptCipher.doFinal(decodedBytes);
// Decode using utf-8
return new String(unencryptedByteArray, "UTF8");
}
public static String encodeBase64(byte[] data) {
if (data == null) {
return null;
}
byte[] encode = Base64.encode(data, Base64.DEFAULT);
String str = null;
try {
str = new String(encode, "UTF-8");
} catch (UnsupportedEncodingException e) {
}
return str;
}
/**
* AES加密
* @param input 加密字符串
* @param key 密钥,密钥必须是16位的
* @return
*/
public static String encrypt(@NonNull String input, @NonNull String key){
byte[] crypted = null;
try{
SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skey);
crypted = cipher.doFinal(input.getBytes());
}catch(Exception e){
System.out.println(e.toString());
}
return new String(Base64.encode(crypted, Base64.DEFAULT));
}
public static byte[] Base64encodingByte(byte[] context,int type) {
byte[] result;
if (judgeVerionOfSdk() > 17) {
result= Base64.encode(context, type);
} else {
result= com.marshalchen.common.commonUtils.urlUtils.Base64.encodeBytesToBytes(context);
}
return result;
}
public static String encrypt(String text) {
if (TextUtils.isEmpty(text)) {
return text;
} else {
return new String(Base64.encode(text.getBytes(UTF8), Base64.NO_WRAP), UTF8);
}
}
private String base64Sha256Hash(byte[] data) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] sha256Bytes = digest.digest(data);
return new String(
Base64.encode(sha256Bytes, Base64.NO_WRAP | Base64.URL_SAFE),
Charset.forName("UTF-8"));
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("SHA-256 not supported on this platform!");
}
}
private String encodeString(String source) {
byte[] bytes = Base64.encode(source.getBytes(), Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING);
return new String(bytes, Charset.defaultCharset());
}
public static byte[] base64Encode(final byte[] input) {
if (input == null || input.length == 0) return new byte[0];
return Base64.encode(input, Base64.NO_WRAP);
}
private static byte[] base64Encode(final byte[] input) {
return Base64.encode(input, Base64.NO_WRAP);
}
private String encodeData(String data) throws UnsupportedEncodingException {
//进行BASE64编码,URL_SAFE/NO_WRAP/NO_PADDING
return new String(Base64.encode(data.getBytes("utf-8"), Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING), "utf-8");
}
@NonNull
public static String encode(String s, String key) {
byte[] xor = xor(s.getBytes(), key.getBytes());
byte[] encoded = Base64.encode(xor, Base64.DEFAULT);
return new String(encoded);
}
public static void send(final Context context, String url, final Object param, final Class clazz, final int TAG) {
if (!url.startsWith("http")) {
url = "http://sanlicun.cn/" + url;
}
Log.d("tag", url);
final RequestQueue requestQueue = Volley.newRequestQueue(context);
final String finalUrl = url;
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("tag", "onResponse:" + response);
if (response.equals("success")) {
index=0;
return;
}
if(index<3){
PayHelperUtils.sendmsg(context,"发送结果失败");
index++;
send(context, finalUrl, param, clazz, TAG);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if(index<3){
PayHelperUtils.sendmsg(context,"发送结果失败");
index++;
send(context, finalUrl, param, clazz, TAG);
}
}
}) {
@Override
protected Map<String, String> getParams() {
//在这里设置需要post的参数
Map<String, String> map = new HashMap<String, String>();
try {
String p = new String(Base64.encode(JSONObject.toJSONString(param).getBytes(), Base64.DEFAULT), "utf-8");
Log.d("tag", p);
map.put("param", p);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return map;
}
};
requestQueue.add(stringRequest);
}
/**
* Base64URL安全编码
* <p>将Base64中的URL非法字符�?,/=转为其他字符, 见RFC3548</p>
*
* @param input 要Base64URL安全编码的字符串
* @return Base64URL安全编码后的字符串
*/
public static byte[] base64UrlSafeEncode(String input) {
return Base64.encode(input.getBytes(), Base64.URL_SAFE);
}
/**
* Base64编码
*
* @param input 要编码的字节数组
* @return Base64编码后的字符串
*/
public static byte[] base64Encode(byte[] input) {
return Base64.encode(input, Base64.NO_WRAP);
}
/**
* Base64编码
*
* @param input 要编码的字节数组
* @return Base64编码后的字符串
*/
private static byte[] base64Encode(byte[] input) {
return Base64.encode(input, Base64.NO_WRAP);
}
/**
* Return Base64-encode bytes.
*
* @param input The input.
* @return Base64-encode bytes
*/
public static byte[] base64Encode(final byte[] input) {
if (input == null || input.length == 0) return new byte[0];
return Base64.encode(input, Base64.NO_WRAP);
}