本帖最后由 liu 于 2018-12-22 17:05 编辑
1、String转换成int
int i = Integer.parseInt(String)
2、String转换成float
Float f = Float.parseFloat(str);
3、String转换成double Double d = Double.parseDouble(str);
4、String转换成byte[]数组 [Java] 纯文本查看 复制代码 public byte[] stringToByteArray(String str, String charset) {
byte[] bytes = null;
if (charset == null) {
bytes = str.getBytes();
} else {
try {
bytes = str.getBytes(charset);
} catch (Exception e) {
}
}
return bytes;
}
5、String转换成bitmap
[Java] 纯文本查看 复制代码 public Bitmap stringToBitmap(String string) {
Bitmap bitmap = null;
try {
byte[] bitmapArray;
bitmapArray = Base64.decode(string, Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
6、其他类型数据转换成String
7、byte[]数组转换成bitmap
[Java] 纯文本查看 复制代码 public Bitmap byteArrayToBitmap(byte[] bytes){
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return bitmap;
}
8、Drawable转换成bitmap
[Java] 纯文本查看 复制代码 public Bitmap drawableToBitmap(Drawable drawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
Bitmap bitmap = bitmapDrawable.getBitmap();
return bitmap;
}
9、bitmap转换成Drawable
[Java] 纯文本查看 复制代码 public Drawable bitmapToDrawable(Bitmap bitmap){
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
Drawable drawable = bitmapDrawable;
return drawable;
}
|