Untitled

 avatar
user_0851248
plain_text
a year ago
1.0 kB
1
Indexable
Never
package com.sdc.humres.android;

import android.os.Build;

import androidx.annotation.RequiresApi;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.util.Base64;

public class ImageUtil {

    @RequiresApi(api = Build.VERSION_CODES.O)
    public static String convertFileToBase64(File file) throws IOException {
        byte[] bytes = Files.readAllBytes(file.toPath());
        return Base64.getEncoder().encodeToString(bytes);
    }


    //Convert a Base64 string and create a file
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static void convertBase64ToFile(String filePath, String fileString, String fileName) throws IOException{
        byte[] bytes = Base64.getDecoder().decode(fileString);
        File file = new File(filePath + "/" +fileName);
        FileOutputStream fop = new FileOutputStream(file);
        fop.write(bytes);
        fop.flush();
        fop.close();
    }
}