既然是初阶篇,为了区分Android上专用和JAVA的通用型分别分开贴出两个类的代码,以后如果发现更好更多将不定期更新
先是Java的基础转换
package com.java.pblib;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
public class UtilsStream {
static Charset defaultcharset = Charset.defaultCharset();
public static byte[] InputStreamToBytes(InputStream in) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024 * 4];
int n = 0;
while ((n = in.read(buffer)) != -1) {
out.write(buffer, 0, n);
}
return out.toByteArray();
}
public static String InputStreamToString(InputStream in, Charset charset) throws IOException {
byte[] buff = InputStreamToBytes(in);
return new String(buff, charset);
}
public static void InputStreamToFile(InputStream in, File file) throws IOException {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[1024 * 4];
while ((bytesRead = in.read(buffer, 0, buffer.length)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
in.close();
}
public static InputStream StringToInputStream(String str, Charset charset) {
ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes(charset));
return stream;
}
public static InputStream FileToInputStream(File file) throws IOException {
return new FileInputStream(file);
}
}
接着是Android上用的常用通用转化
package com.android.pblib;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import android.content.Context;
import android.graphics.*;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
public class UtilsConvert {
public static Bitmap InputStreamToBitmap(InputStream in) {
return BitmapFactory.decodeStream(in);
}
public static Bitmap BytesToBitmap(byte[] b) {
if (b.length != 0)
return BitmapFactory.decodeByteArray(b, 0, b.length);
else
return null;
}
public static Bitmap DrawableToBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
public static Bitmap GetBitmapByResId(Context context,int resid){
Bitmap bmp=BitmapFactory.decodeResource(context.getResources(), resid);
return null;
}
public static byte[] BimapTpBytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
public static InputStream BitmapToInputStream(Bitmap bm, int quality) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, quality, baos);
InputStream is = new ByteArrayInputStream(baos.toByteArray());
return is;
}
// Bitmap转换成Drawable
public static Drawable BitmapToDrawable(Bitmap bitmap) {
BitmapDrawable bd = new BitmapDrawable(bitmap);
Drawable d = (Drawable) bd;
return d;
}
}