Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.File;

import java.io.FileOutputStream;

import java.io.OutputStream;
import android.os.Environment;

public class Main {
    private static String APP_DIR = "java2s";
    public static final File EXTERNAL_STORAGE_DIRECTORY = Environment.getExternalStorageDirectory();

    public static OutputStream getOutStreamFromFile(String appath, String filename) {
        File f = getFile(appath, filename);
        return getOutStreamFromFile(f);
    }

    public static OutputStream getOutStreamFromFile(File file) {
        if (file != null) {
            try {
                return new FileOutputStream(file);
            } catch (Exception ex) {
                return null;
            }
        } else {
            return null;
        }
    }

    public static File getFile(String appath, String filename) {
        if (checkDir(appath) == false) {
            return null;
        }

        try {
            String file = getFilePath(appath, filename);
            File fileObj = new File(file);
            return fileObj;
        } catch (SecurityException ex) {
            return null;
        }
    }

    public static File getFile(String file) {
        return getFile(null, file);
    }

    public static boolean checkDir(String appath) {
        String dir = getPath(appath);

        if (checkSD() == false) {
            return false;
        }
        File tf = new File(dir);

        if (tf.exists()) {
            return true;
        }

        boolean ret = tf.mkdirs();
        if (ret == false) {
            return false;
        }

        return true;
    }

    public static String getFilePath(String appath, String filename) {
        String file = null;
        if (appath != null) {
            file = EXTERNAL_STORAGE_DIRECTORY + "/" + APP_DIR + "/" + appath + "/" + filename;
        } else {
            file = EXTERNAL_STORAGE_DIRECTORY + "/" + APP_DIR + "/" + filename;
        }
        return file;
    }

    public static String getFilePath(String file) {
        return getFilePath(null, file);
    }

    public static String getPath(String appath) {
        String path = null;
        if (appath != null) {
            path = EXTERNAL_STORAGE_DIRECTORY + "/" + APP_DIR + "/" + appath + "/";
        } else {
            path = EXTERNAL_STORAGE_DIRECTORY + "/" + APP_DIR + "/";
        }
        return path;
    }

    public static boolean checkSD() {
        String status = Environment.getExternalStorageState();
        if (status.equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }
}