InputStream in = null;
byte[] data = null;
//讀取圖片字節數組
try
{
in = new FileInputStream(你的文件路徑); //該路徑只能是本機路徑,不能是網絡地址
data = new byte[in.available()];
in.read(data);
}
catch (IOException e)
{
e.printStackTrace();
} finally{
try {
if(in != null){
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
BASE64Encoder encoder = new BASE64Encoder();
String code = "data:image/png;base64,"+encoder.encode(data).replaceAll("\n|\r","");