List of usage examples for android.content Context getExternalFilesDirs
public abstract File[] getExternalFilesDirs(String type);
From source file:com.androidzeitgeist.dashwatch.common.IOUtil.java
@TargetApi(Build.VERSION_CODES.KITKAT) public static File getBestAvailableFilesRoot(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // In KitKat we can query multiple devices. // TODO: optimize for stability instead of picking first one File[] roots = context.getExternalFilesDirs(null); if (roots != null) { for (File root : roots) { if (root == null) { continue; }//from w ww . j a v a 2s .com if (Environment.MEDIA_MOUNTED.equals(Environment.getStorageState(root))) { return root; } } } } else if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { // Pre-KitKat, only one external storage device was addressable return context.getExternalFilesDir(null); } // Worst case, resort to internal storage return context.getFilesDir(); }
From source file:com.filemanager.free.filesystem.FileUtil.java
@TargetApi(Build.VERSION_CODES.KITKAT) public static String[] getExtSdCardPathsForActivity(Context context) { List<String> paths = new ArrayList<String>(); for (File file : context.getExternalFilesDirs("external")) { if (file != null) { int index = file.getAbsolutePath().lastIndexOf("/Android/data"); if (index < 0) { Log.w("AmazeFileUtils", "Unexpected external file dir: " + file.getAbsolutePath()); } else { String path = file.getAbsolutePath().substring(0, index); try { path = new File(path).getCanonicalPath(); } catch (IOException e) { // Keep non-canonical path. }// www . j a v a 2 s. c om paths.add(path); } } } if (paths.isEmpty()) paths.add("/storage/sdcard1"); return paths.toArray(new String[0]); }
From source file:com.filemanager.free.filesystem.FileUtil.java
/** * Get a list of external SD card paths. (Kitkat or higher.) * * @return A list of external SD card paths. *//*from ww w .j a va 2 s. co m*/ @TargetApi(Build.VERSION_CODES.KITKAT) public static String[] getExtSdCardPaths(Context context) { List<String> paths = new ArrayList<String>(); for (File file : context.getExternalFilesDirs("external")) { if (file != null && !file.equals(context.getExternalFilesDir("external"))) { int index = file.getAbsolutePath().lastIndexOf("/Android/data"); if (index < 0) { Log.w("FileUtils", "Unexpected external file dir: " + file.getAbsolutePath()); } else { String path = file.getAbsolutePath().substring(0, index); try { path = new File(path).getCanonicalPath(); } catch (IOException e) { // Keep non-canonical path. } paths.add(path); } } } if (paths.isEmpty()) paths.add("/storage/sdcard1"); return paths.toArray(new String[0]); }
From source file:com.pdftron.pdf.utils.Utils.java
@TargetApi(Build.VERSION_CODES.LOLLIPOP) public static boolean isSdCardFile(Context context, File file) { if (!Utils.isKitKat()) { // not applicable for below kitkat return false; }/*from w ww. j a va 2s . c o m*/ if (file != null) { if (file.getParentFile() == null || file.getAbsolutePath().equals("/storage")) { // File cannot be on a SD-card return false; } final File storageDirectory = Environment.getExternalStorageDirectory(); final File[] rootDirs = context.getExternalFilesDirs(null); if (rootDirs != null && rootDirs.length > 0) { for (File dir : rootDirs) { if (dir != null) { try { if (!FilenameUtils.equals(storageDirectory.getAbsolutePath(), dir.getAbsolutePath()) && !FileUtils.directoryContains(storageDirectory, dir)) { while (dir.getParentFile() != null && !dir.getAbsolutePath().equalsIgnoreCase("/storage")) { if (FilenameUtils.equals(file.getAbsolutePath(), dir.getAbsolutePath()) || FileUtils.directoryContains(dir, file)) { // The current folder is on the SD-card return true; } dir = dir.getParentFile(); } } } catch (IOException ignored) { } } } } } return false; }
From source file:com.xperia64.timidityae.Globals.java
@SuppressLint("NewApi") public static String[] getDocFilePaths(Context c, String parent) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return null; // Error. parent = parent.replace("//", "/"); String probablyTheDirectory = ""; String probablyTheRoot = ""; File par = new File(parent); String absp = par.getAbsolutePath(); File[] x = c.getExternalFilesDirs(null); for (File f : x) { if (f != null) { String ex = f.getAbsolutePath(); String ss1;//from w w w . ja v a 2s . c o m String ss2; int lastmatch = 1; while (lastmatch < absp.length() && lastmatch < ex.length()) { ss1 = ex.substring(0, lastmatch + 1); ss2 = absp.substring(0, lastmatch + 1); if (ss1.equals(ss2)) { lastmatch++; } else { break; } } String theRoot = absp.substring(0, lastmatch); if (theRoot.equals("/storage/") || theRoot.equals("/mnt/")) { continue; } else { probablyTheDirectory = ex; probablyTheRoot = theRoot; break; } } } String[] rets = new String[2]; rets[0] = probablyTheDirectory; rets[1] = probablyTheRoot; return rets; }
From source file:eu.mhutti1.utils.storage.StorageDeviceUtils.java
public static ArrayList<StorageDevice> getStorageDevices(Context context, boolean writable) { mStorageDevices = new ArrayList<>(); // Add as many possible mount points as we know about // Only add this device if its very likely that we have missed a users sd card if (Environment.isExternalStorageEmulated()) { // This is our internal storage directory mStorageDevices.add(new StorageDevice( generalisePath(Environment.getExternalStorageDirectory().getPath(), writable), true)); } else {// ww w . j av a 2 s . co m // This is the internal directory of our app that only we can write to mStorageDevices.add(new StorageDevice(context.getFilesDir().getPath(), true)); // This is an external storage directory mStorageDevices.add(new StorageDevice( generalisePath(Environment.getExternalStorageDirectory().getPath(), writable), false)); } // These are possible manufacturer sdcard mount points String[] paths = ExternalPaths.getPossiblePaths(); for (String path : paths) { if (path.endsWith("*")) { File root = new File(path.substring(0, path.length() - 1)); File[] directories = root.listFiles(new FileFilter() { @Override public boolean accept(File file) { return file.isDirectory(); } }); if (directories != null) { for (File dir : directories) { mStorageDevices.add(new StorageDevice(dir, false)); } } } else { mStorageDevices.add(new StorageDevice(path, false)); } } // Iterate through any sdcards manufacturers may have specified in Kitkat+ and add them if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { for (File file : context.getExternalFilesDirs("")) { if (file != null) { mStorageDevices.add(new StorageDevice(generalisePath(file.getPath(), writable), false)); } } } // Check all devices exist, we can write to them if required and they are not duplicates return checkStorageValid(writable); }