Android Base64 File Decode decodeToFile(String filePath, String base64)

Here you can find the source of decodeToFile(String filePath, String base64)

Description

decode To File

Declaration

public static void decodeToFile(String filePath, String base64)
            throws Exception 

Method Source Code

//package com.java2s;
import java.io.ByteArrayInputStream;

import java.io.File;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import android.util.Base64;

public class Main {
    private static final int CACHE_SIZE = 1024;

    public static void decodeToFile(String filePath, String base64)
            throws Exception {
        byte[] bytes = decode(base64);
        bytesToFile(bytes, filePath);/*from   w ww . j  a  va2 s  .co  m*/
    }

    public static byte[] decode(String base64) throws Exception {
        return Base64.decode(base64.getBytes(), Base64.DEFAULT);
    }

    public static void bytesToFile(byte[] bytes, String filePath)
            throws Exception {
        InputStream in = new ByteArrayInputStream(bytes);
        File destFile = new File(filePath);
        if (!destFile.getParentFile().exists()) {
            destFile.getParentFile().mkdirs();
        }
        destFile.createNewFile();
        OutputStream out = new FileOutputStream(destFile);
        byte[] cache = new byte[CACHE_SIZE];
        int nRead = 0;
        while ((nRead = in.read(cache)) != -1) {
            out.write(cache, 0, nRead);
            out.flush();
        }
        out.close();
        in.close();
    }
}

Related

  1. decodeToFile(String filePath, String base64)
  2. decoderBase64FileWithFileName(String base64Code, String fileName)