Android Byte Array Save to File writeByteArrayToSD(String path, byte[] content, boolean create)

Here you can find the source of writeByteArrayToSD(String path, byte[] content, boolean create)

Description

write Byte Array To SD

License

Apache License

Declaration

public static void writeByteArrayToSD(String path, byte[] content,
        boolean create) 

Method Source Code

//package com.java2s;
/*//from w  w  w .j a  va 2  s.c om
 * Copyright (C) 2013 www.418log.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;

import java.io.FileOutputStream;

import android.os.Environment;

public class Main {

    public static void writeByteArrayToSD(String path, byte[] content,
            boolean create) {

        FileOutputStream fos = null;
        try {
            File file = new File(path);
            if (!isCanUseSD()) {
                return;
            }
            if (!file.exists()) {
                if (create) {
                    File parent = file.getParentFile();
                    if (!parent.exists()) {
                        parent.mkdirs();
                        file.createNewFile();
                    }
                } else {
                    return;
                }
            }
            fos = new FileOutputStream(path);
            fos.write(content);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (Exception e) {
                }
            }
        }
    }

    public static boolean isCanUseSD() {
        try {
            return Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}

Related

  1. writeFile(String fileName, byte[] datas, boolean overwrite)
  2. writeFile(String path, String fileName, byte[] datas, boolean overwrite)
  3. saveBytes(byte[] bytes, File file)
  4. saveBytes(byte[] bytes, OutputStream out)
  5. saveBytes(byte[] bytes, String file)