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 String getOutputAudioPath(Context context, String mediaName) {
    File mediaStorageDir = new File(context.getExternalFilesDir(Environment.DIRECTORY_RINGTONES),
            AudioFolderName);/*from   w  w  w  .j a  v  a  2s .  com*/

    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            return null;
        }
    }

    File noMediaFile = new File(mediaStorageDir, ".nomedia");
    if (!noMediaFile.exists()) {
        try {
            noMediaFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return mediaStorageDir.getPath() + File.separator + mediaName;
}

From source file:Main.java

public static String readFileText(Context context, String fileName, boolean deleteFile) {
    File root = new File(context.getExternalFilesDir(null) + File.separator);
    File file = new File(root, "." + fileName);

    StringBuilder text = new StringBuilder();

    if (file.exists()) {
        try {//from  w  w  w. j  ava 2s .  com
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;

            while ((line = br.readLine()) != null) {
                text.append(line);
                text.append('\n');
            }

            br.close();

            if (deleteFile)
                file.delete();
        } catch (IOException e) {

        }
    } else {
        text.append("File not exists");
    }

    return text.toString();
}

From source file:Main.java

public static String getFileRoot(Context context) {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        File external = context.getExternalFilesDir(null);
        if (external != null) {
            return external.getAbsolutePath();
        }//  ww  w . j av a2  s  .c  om
    }
    return context.getFilesDir().getAbsolutePath();
}

From source file:Main.java

public static File getWritableFileDir(Context context) {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        File file = context.getExternalFilesDir(null);
        if (file == null) {
            file = context.getFilesDir();
        }//  w  w  w.j  a  v  a2s .c o m
        return file;
    } else {
        return context.getFilesDir();
    }
}

From source file:Main.java

public static String getDownAudioPath(Context context, String url) {
    String fileName = url.substring(url.lastIndexOf("/") + 1);
    File mediaStorageDir = new File(context.getExternalFilesDir(Environment.DIRECTORY_RINGTONES),
            AudioFolderName);/* ww w .ja va 2s. c  o  m*/

    return mediaStorageDir.getPath() + File.separator + fileName;
}

From source file:Main.java

private static String getFileRoot(Context context) {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        File external = context.getExternalFilesDir(null);
        if (external != null) {
            return external.getAbsolutePath();
        }//from www  . ja v a  2 s .  c o  m
    }

    return context.getFilesDir().getAbsolutePath();
}

From source file:Main.java

public static String getSaveImgPath(Context context, String url) {
    //      String fileName = url.substring(url.lastIndexOf("/") + 1);
    File mediaStorageDir = new File(context.getExternalFilesDir(Environment.DIRECTORY_RINGTONES), SaveImg);

    return mediaStorageDir.getPath();
    //            + File.separator + fileName;
}

From source file:Main.java

public static File getOutputMediaFile(Context context, String mediaName) {
    File mediaStorageDir = null;/*ww  w  .j  ava2  s .  c o  m*/
    try {
        mediaStorageDir = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), ImgFolderName);
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            return null;
        }
    }

    File mediaFile = new File(mediaStorageDir.getPath() + File.separator + mediaName);
    return mediaFile;
}

From source file:Main.java

public static File getStoragePath(Context context) {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        File externalStorage = context.getExternalFilesDir(null);
        if (externalStorage == null)
            return null;
        return new File(externalStorage.getPath() + File.separatorChar + "storage");
    }//from  ww  w  .j  av a 2s.  co  m

    return null;
}

From source file:Main.java

public static File getCpoFilesDir(Context appCtx) {

    File filesDir;//from  w  ww .  j  av  a2  s.  c  o m

    if (isExternalStorageWritable()) {
        // We can read and write the media
        filesDir = appCtx.getExternalFilesDir(null);
    } else {
        // Load another directory, probably local memory
        filesDir = appCtx.getFilesDir();
    }

    return filesDir;
}