android 图片字节码转换

来源:互联网 发布:重生之软件帝国 编辑:程序博客网 时间:2024/05/30 02:25
  1. public static byte[] imageToBytes(String path) throws IOException {  
  2.     InputStream inputStream = new FileInputStream(path);  
  3.     BufferedInputStream in = new BufferedInputStream(inputStream);  
  4.     ByteArrayOutputStream out = new ByteArrayOutputStream(1024);  
  5.   
  6.     byte[] temp = new byte[1024];  
  7.     int size = 0;  
  8.     while ((size = in.read(temp)) != -1) {  
  9.         out.write(temp, 0, size);  
  10.     }  
  11.     in.close();  
  12.     byte[] content = out.toByteArray();  
  13.     return content;  
  14. }  

//图片转字节码

 

public Bitmap stringtoBitmap(String string){
    //将字符串转换成Bitmap类型
    Bitmap bitmap=null;
    try {
    byte[]bitmapArray;
    bitmapArray=Base64.decode(string, Base64.DEFAULT);
bitmap=BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
} catch (Exception e) {
e.printStackTrace();
}
   //字节码转图片

原创粉丝点击