我如何以字节形式读取文件,然后在 java ( android ) 中将其编码为 base64

how i can read files as bytes then encode it to base64 in java ( android )

我如何以字节形式读取文件,然后通过 java 以低 api 将其编码为 base64,例如:api 级别 17?

我尝试使用此代码将文件转换为 base64:

Java Encode file to Base64 string To match with other encoded string

File file = new File(filepath);
byte[] fileContent = Files.readAllBytes(file.toPath());
String encodedString = Base64.getEncoder().encodeToString(fileContent.getBytes());

旧 api 使用 FileUtils.readFileToByteArray(File file) 要么 IOUtils.toByteArray(InputStream input).