根据图片文件路径,把图片转化为base64

public static String encodeBase64File(String path) throws IOException {
    String type = path.substring(path.lastIndexOf(".") + 1);
    if (type.equals("jpg") || type.equals("png")) {
        Bitmap bm = getSmallBitmap(path);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        if (null == bm) {
            return null;
        }
        bm.compress(Bitmap.CompressFormat.JPEG, 80, baos);
        byte[] b = baos.toByteArray();
        return Base64.encodeToString(b, Base64.DEFAULT);
    } else {
        File file = new File(path);
        FileInputStream inputFile = new FileInputStream(file);
        byte[] buffer = new byte[(int) file.length()];
        inputFile.read(buffer);
        inputFile.close();
        return Base64.encodeToString(buffer, Base64.DEFAULT);
    }
}

你可能感兴趣的:(andrroid)