android 1、Base64字符串转换成图片 2、二进制流转换为Bitmap图片 3、Bitmap转换为二进制流 4、String路径图片转二进制

TODO1:将字符串转换成Bitmap类型(Base64字符串转换成图片)

 public Bitmap stringtoBitmap(String imgBase64){
     Bitmap bitmap=null;
     try {
         byte[]bitmapArray;
         bitmapArray=Base64.decode(imgBase64, Base64.DEFAULT);
         bitmap=BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
     } catch (Exception e) {
         e.printStackTrace();
     }
     return bitmap;
 }

TODO2:二进制流转换为Bitmap图片

    public Bitmap getBitmapFromByte(byte[] temp) {
        if (temp != null) {
            Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length);
            return bitmap;
        } else {
            return null;
        }
    }

TODO3:Bitmap转换为二进制流

    private static byte[] bitmapToByte(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] imgBytes = baos.toByteArray();
        return imgBytes;
    }

TODO4:String路径图片转二进制

    /**
     * 照片转byte二进制
     *
     * @param imagepath 需要转byte的照片路径
     * @return 已经转成的byte
     * @throws Exception
     */
    public static byte[] readStream(String imagepath) throws Exception {
        FileInputStream fs = new FileInputStream(imagepath);
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while (-1 != (len = fs.read(buffer))) {
            outStream.write(buffer, 0, len);
        }
        outStream.close();
        fs.close();
        return outStream.toByteArray();
    }

@希望能帮助到大家

你可能感兴趣的:(Android)