本文整理汇总了Java中android.util.Base64.encode方法的典型用法代码示例。如果您正苦于以下问题:Java Base64.encode方法的具体用法?Java Base64.encode怎么用?Java Base64.encode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.util.Base64
的用法示例。
在下文中一共展示了Base64.encode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: encrypt
import android.util.Base64; //导入方法依赖的package包/类
static String encrypt(String plaintext, String key, EncodingType encodingType) throws UnsupportedEncodingException {
// Set up cipher algo
RNCryptorNative c = new RNCryptorNative();
// Encode the plaintext
byte[] plaintextBytes = Base64.encode(plaintext.getBytes("utf-8"), Base64.DEFAULT);
// Encrypt
byte[] ciphertextBytes = c.encrypt(new String(plaintextBytes), formatKey(key));
String ret;
switch (encodingType) {
case HEX:
ret = EncodingHelper.bytesToHex(ciphertextBytes);
ret = ret.toLowerCase();
break;
case BASE64:
ret = Base64.encodeToString(ciphertextBytes, Base64.DEFAULT);
break;
default: // Base64 by default
ret = Base64.encodeToString(ciphertextBytes, Base64.DEFAULT);
}
return ret;
}
示例2: 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;
}
}
示例3: processPicture
import android.util.Base64; //导入方法依赖的package包/类
/**
* Compress bitmap using jpeg, convert to Base64 encoded string, and return to JavaScript.
*
* @param bitmap
*/
public void processPicture(Bitmap bitmap, int encodingType) {
ByteArrayOutputStream jpeg_data = new ByteArrayOutputStream();
CompressFormat compressFormat = encodingType == JPEG ?
CompressFormat.JPEG :
CompressFormat.PNG;
try {
if (bitmap.compress(compressFormat, mQuality, jpeg_data)) {
byte[] code = jpeg_data.toByteArray();
byte[] output = Base64.encode(code, Base64.NO_WRAP);
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;
}
示例4: 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).apply();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
示例5: saveDeviceData
import android.util.Base64; //导入方法依赖的package包/类
/**
* 将对象储存到 SharedPreference
*
* @param context Context
* @param key Key
* @param device 自定义类对象
*/
public static <T> boolean saveDeviceData(Context context, String key, T device) {
if (mSharedPreferences == null) {
mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
// 创建对象输出流,并封装字节流
ObjectOutputStream oos = new ObjectOutputStream(stream);
// 将对象写入字节流
oos.writeObject(device);
// 将字节流编码成base64的字符串
String oauthBase64 = new String(Base64.encode(stream
.toByteArray(), Base64.DEFAULT));
mSharedPreferences.edit().putString(key, oauthBase64).apply();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
示例6: doInBackground
import android.util.Base64; //导入方法依赖的package包/类
/**
* This is an asynchronously called function, that will send an HTTP POST request to the
* translator endpoint with the Korean text in the request.
* @return String response from the translator service.
*/
@Override
protected String doInBackground(String... params) {
String result = "";
try {
URL url = new URL(TRANSLATE_API_ENDPOINT);
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestMethod("POST");
// Set authorization header.
String authString = this.username + ":" + this.password;
byte[] base64Bytes = Base64.encode(authString.getBytes(), Base64.DEFAULT);
String base64String = new String(base64Bytes);
urlConnection.setRequestProperty("Authorization", "Basic " + base64String);
if (this.postData != null) {
OutputStreamWriter writer = new OutputStreamWriter(urlConnection.getOutputStream());
writer.write(postData.toString());
writer.flush();
writer.close();
}
int statusCode = urlConnection.getResponseCode();
if (statusCode == 200) {
InputStreamReader streamReader = new InputStreamReader(
urlConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(streamReader);
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = bufferedReader.readLine()) != null) {
response.append(inputLine);
}
streamReader.close();
bufferedReader.close();
result = response.toString();
}
else {
System.out.println("Error translating. Response Code: " + statusCode);
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
return result;
}
示例7: getActions
import android.util.Base64; //导入方法依赖的package包/类
private void getActions(final DeviceDAO device) {
Log.d(TAG, "TODO getActions");
String uri = Uri.parse(String.format("http://%s:%s/", device.getIP(), device.getPort()))
.buildUpon().build().toString();
String credentials = device.getUsername() + ":" + device.getPassword();
byte[] t = credentials.getBytes();
byte[] auth = Base64.encode(t, Base64.DEFAULT);
final String basicAuthValue = new String(auth);
requestQueue.add(new StringRequest(Request.Method.POST, uri, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Authorization", "Basic " + basicAuthValue);
params.put("Connection", "close");
return params;
}
});
}
示例8: getGeoHeader
import android.util.Base64; //导入方法依赖的package包/类
/**
* 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;
}
示例9: encodeBase64Ex
import android.util.Base64; //导入方法依赖的package包/类
private static byte[] encodeBase64Ex(byte[] src) {
// urlsafe version is not supported in version 1.4 or lower.
byte[] b64 = Base64.encode(src, Base64.NO_WRAP);
for (int i = 0; i < b64.length; i++) {
if (b64[i] == '/') {
b64[i] = '_';
} else if (b64[i] == '+') {
b64[i] = '-';
}
}
return b64;
}
示例10: encrypt
import android.util.Base64; //导入方法依赖的package包/类
protected String encrypt(String value ) {
try {
final byte[] bytes = value!=null ? value.getBytes(UTF8) : new byte[0];
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key = keyFactory.generateSecret(new PBEKeySpec(SEKRIT));
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(Secure.getString(context.getContentResolver(), Secure.ANDROID_ID).getBytes(UTF8), 20));
return new String(Base64.encode(pbeCipher.doFinal(bytes), Base64.NO_WRAP),UTF8);
} catch( Exception e ) {
throw new RuntimeException(e);
}
}
示例11: encrypt
import android.util.Base64; //导入方法依赖的package包/类
public String encrypt(String plainText) throws Exception {
cipher.init(Cipher.ENCRYPT_MODE, key, spec);
byte[] encrypted = cipher.doFinal(plainText.getBytes("UTF-8"));
String encryptedText = new String(Base64.encode(encrypted,
Base64.DEFAULT), "UTF-8");
return encryptedText;
}
示例12: encrypt
import android.util.Base64; //导入方法依赖的package包/类
@Override
public byte[] encrypt(byte[] res) {
if(cipher != null) res = cipher.encrypt(res);
return Base64.encode(res, Base64.DEFAULT);
}
示例13: base64UrlSafeEncode
import android.util.Base64; //导入方法依赖的package包/类
/**
* 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);
}
示例14: base64Encode
import android.util.Base64; //导入方法依赖的package包/类
/**
* Base64编码
*
* @param input 要编码的字节数组
* @return Base64编码后的字符串
*/
private static byte[] base64Encode(byte[] input)
{
return Base64.encode(input, Base64.NO_WRAP);
}
示例15: base64Encode
import android.util.Base64; //导入方法依赖的package包/类
/**
* Base64编码
*
* @param input 要编码的字节数组
* @return Base64编码后的字符串
*/
private static byte[] base64Encode(byte[] input) {
return Base64.encode(input, Base64.NO_WRAP);
}