Android 文件转base64字符串

/**
 * 文件转base64字符串
 *
 * @param file
 * @return
 */
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;
}

猜你喜欢

转载自blog.csdn.net/meixi_android/article/details/82182686