Example usage for android.content Context getExternalFilesDir

List of usage examples for android.content Context getExternalFilesDir

Introduction

In this page you can find the example usage for android.content Context getExternalFilesDir.

Prototype

@Nullable
public abstract File getExternalFilesDir(@Nullable String type);

Source Link

Document

Returns the absolute path to the directory on the primary shared/external storage device where the application can place persistent files it owns.

Usage

From source file:Main.java

public static void createNomedia(Context context) {
    try {/*from w ww.  j av a2 s.c  o m*/
        File file = new File(context.getExternalFilesDir(null).getAbsolutePath() + NOMEDIA_FILE);
        //file.mkdirs(); ???
        if (!file.exists()) {
            file.createNewFile();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String getOldAudioRootDirectory(Context context) {
    String sep = File.separator;

    File f = context.getExternalFilesDir(null);
    String path = sep + "audio" + sep;

    if (f == null) {
        return null;
    }/*w ww.  j a v  a  2 s .  c  o  m*/
    return f.getAbsolutePath() + path;
}

From source file:Main.java

static public File getPath(Context ctx, boolean externalStorage) {
    if (externalStorage) {
        return ctx.getExternalFilesDir(null);
    } else {/*ww  w .j  a  va  2s.com*/
        return ctx.getFilesDir();
    }
}

From source file:Main.java

public static void logScanReport(Context c, String[] logs) {
    String dir = c.getExternalFilesDir(null).getParent();
    try {/* w w w .j  a v  a 2s  .  c o m*/
        File file = new File(dir + "/last_scan.log");
        boolean append = false;
        // if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        } else {
            /*
            if(file.length() >= 2097152){
               // set log file max size to 2mb
               append = false;
            }
             */
        }
        FileOutputStream fos = new FileOutputStream(file, append);

        for (String log : logs) {
            fos.write((log + "\n\n").getBytes());
        }
        fos.close();
    } catch (IOException ioe) {
        Log.e("AndroidUtils", "An exception has occured in logScanReport!");
        ioe.printStackTrace();
    }
}

From source file:Main.java

private static File getStorageFolder(Context context) {
    // Get external storage folder
    File storageFolder = context.getExternalFilesDir(null);
    if (storageFolder == null) {
        // If failed, get internal storage folder
        storageFolder = context.getFilesDir();
    }//  www . ja va2s . co  m
    return storageFolder;
}

From source file:Main.java

public static void logProgressReport(Context c, String[] logs) {
    String dir = c.getExternalFilesDir(null).getParent();
    try {//from   w  w  w .j ava 2 s.com
        File file = new File(dir + "/sf_reports.log");
        boolean append = false;
        // if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        } else {
            /*
            if(file.length() >= 2097152){
               // set log file max size to 2mb
               append = false;
            }
             */

        }
        FileOutputStream fos = new FileOutputStream(file, append);

        for (String log : logs) {
            fos.write((log + "\n\n").getBytes());
        }
        fos.close();
    } catch (IOException ioe) {
        Log.e("AndroidUtils", "An exception has occured in logProgressReport!");
        ioe.printStackTrace();
    }
}

From source file:Main.java

public static String getExternalFilesDir(Context context) {
    if (sExternalFilesDir == null)
        sExternalFilesDir = context.getExternalFilesDir(null).getAbsolutePath();
    return sExternalFilesDir;
}

From source file:nz.ac.auckland.lablet.ExperimentAnalysisBaseActivity.java

static public File getDefaultExperimentBaseDir(Context context) {
    File baseDir = context.getExternalFilesDir(null);
    return new File(baseDir, "experiments");
}

From source file:Main.java

public static String createExternalStoragePrivateFile(Context appCtx, InputStream is, String fileName) {

    File file = new File(appCtx.getExternalFilesDir(null), fileName);

    try {/* w ww . ja va2  s. c  o  m*/
        //InputStream is = appCtx.getResources().openRawResource(R.raw.calipso);
        OutputStream os = new FileOutputStream(file);
        byte[] data = new byte[is.available()];
        is.read(data);
        os.write(data);
        is.close();
        os.close();
    } catch (IOException e) {
        // Unable to create file, likely because external storage is
        // not currently mounted.
        Log.w("ExternalStorage", "Error writing " + file, e);
    }

    return file.getPath();
}

From source file:Main.java

public static String getSdCacheDir(Context context) {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

        if (true) {
            Context appContext = context.getApplicationContext();

            File amap = appContext.getExternalFilesDir("offlineMap");
            return amap.getAbsolutePath() + "/";
        } else {//from w ww .java  2 s  .  co  m

            File fExternalStorageDirectory = Environment.getExternalStorageDirectory();
            File autonaviDir = new File(fExternalStorageDirectory, "amapsdk");
            boolean result = false;
            if (!autonaviDir.exists()) {
                result = autonaviDir.mkdir();
            }
            File minimapDir = new File(autonaviDir, "offlineMap");
            if (!minimapDir.exists()) {
                result = minimapDir.mkdir();
            }
            return minimapDir.toString() + "/";
        }

    } else {
        return "";
    }
}