文件,base64字符串,Bitmap的相互转

文件,base64字符串,Bitmap的相互转


————————————————
原文链接:https://blog.csdn.net/fengyeNom1/article/details/90024586

public class Base64Util {
public static void gcBitmap(Bitmap bitmap) {
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle(); // 回收图片所占的内存
bitmap = null;
System.gc(); // 提醒系统及时回收
}
}

/**
 * Bitmap To Base64
 */
public static String bitmapToBase64(Bitmap bitmap) {
    // 要返回的字符串
    String reslut = null;
    ByteArrayOutputStream baos = null;
    try {
        if (bitmap != null) {
            baos = new ByteArrayOutputStream();
            //todo 压缩只对保存有效果bitmap还是原来的大小
            bitmap.compress(Bitmap.CompressFormat.JPEG, 30, baos);
            baos.flush();
            baos.close();
            // 转换为字节数组
            byte[] byteArray = baos.toByteArray();
            // 转换为字符串
            reslut = Base64.encodeToString(byteArray, Base64.NO_WRAP);
        } else {
            return null;
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (baos != null) {
                baos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return reslut;
}

/**
 * Bitmap To Bitmap
 */
public static Bitmap base64ToBitmap(String base64String) {
    byte[] decode = Base64.decode(base64String, Base64.DEFAULT);
    Bitmap bitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length);
    return bitmap;
}

/**
 * Bitmap 转化为 二维数组
 */
public static byte[] getBytesByBitmap(Bitmap bitmap) {
    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(bitmap.getByteCount());
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
        return outputStream.toByteArray();
    } catch (Exception e) {
        return new byte[10];
    }
}

/**
 * 文件 转 base64字符串
 */
public static String fileToBase64(File file) {
    String base64 = null;
    InputStream in = null;
    try {
        in = new FileInputStream(file);
        byte[] bytes = new byte[in.available()];
        int length = in.read(bytes);
        base64 = Base64.encodeToString(bytes, 0, length, Base64.DEFAULT);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return base64;
}

/**
 * base64 字符串 转 文件
 */
public static File base64ToFile(String base64) {
    File file = null;
    String fileName = "/Petssions/record/testFile.amr";
    FileOutputStream out = null;
    try {
        // 解码,然后将字节转换为文件
        file = new File(Environment.getExternalStorageDirectory(), fileName);
        if (!file.exists()) {
            file.createNewFile();
        }
        // 将字符串转换为byte数组
        byte[] bytes = Base64.decode(base64, Base64.DEFAULT);
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        byte[] buffer = new byte[1024];
        out = new FileOutputStream(file);
        int bytesum = 0;
        int byteread = 0;
        while ((byteread = in.read(buffer)) != -1) {
            bytesum += byteread;
            // 文件写操作
            out.write(buffer, 0, byteread);
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return file;
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值