base64相关

58 阅读1分钟

代码

public static String imageToBase64(String path, int base64Flag) {    if (TextUtils.isEmpty(path)) {        return null;    } else {        InputStream is = null;        byte[] data = null;        String result = null;        try {            is = Files.newInputStream(Paths.get(path));            byte[] data = new byte[is.available()];            is.read(data);            result = Base64.encodeToString(data, base64Flag);        } catch (Exception var14) {            Exception e = var14;            e.printStackTrace();        } finally {            if (null != is) {                try {                    is.close();                } catch (IOException var13) {                    IOException e = var13;                    e.printStackTrace();                }            }        }        return result;    }}

如果是传给前端,需要加前缀NSString *prefix = @"data:image/jpeg;base64,";

部分机型有兼容问题,需要base64-nowrap格式的,比如是一加手机,否则他们会不识别