Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.File;
import android.content.Context;
import android.os.Environment;
import android.util.Log;

public class Main {
    public static String getAuditoryStage2SubDir(Context ctx, String subPath) {

        String fullPath = getStoragePath("/auditory" + "/stage2" + "/" + subPath, ctx);
        Log.d("fullPath", fullPath);
        return fullPath;
    }

    public static String getStoragePath(String path, Context ctx) {
        if (isExternalStorageMounted()) {
            return getExternalStoragePathByContext(path, ctx);
        } else {
            return getInternalStoragePathByContext(path, ctx);
        }
    }

    private static boolean isExternalStorageMounted() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    private static String getExternalStoragePathByContext(String path, Context context) {
        File dirPath = context.getExternalFilesDir(path);
        if (dirPath == null) {
            dirPath = new File(context.getExternalFilesDir(null).getAbsolutePath() + "/" + path + "/");
        }

        if (!dirPath.exists()) {
            dirPath.mkdir();
        }
        return dirPath.getAbsolutePath();
    }

    private static String getInternalStoragePathByContext(String path, Context context) {
        File dirPath = context.getDir(path, Context.MODE_PRIVATE);
        if (dirPath.exists()) {
            dirPath.mkdir();
        }
        return dirPath.getAbsolutePath();
    }
}