android.content.Context#getExternalFilesDirs ( )源码实例Demo

下面列出了android.content.Context#getExternalFilesDirs ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: PowerFileExplorer   文件: FileUtil.java
/**
 * Get a list of external SD card paths. (Kitkat or higher.)
 *
 * @return A list of external SD card paths.
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
private static String[] getExtSdCardPaths(Context context) {
    List<String> paths = new ArrayList<>();
    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("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.
                }
                paths.add(path);
            }
        }
    }
    if (paths.isEmpty()) paths.add("/storage/sdcard1");
    return paths.toArray(new String[0]);
}
 
源代码2 项目: PowerFileExplorer   文件: FileUtil.java
@TargetApi(Build.VERSION_CODES.KITKAT)
public static String[] getExtSdCardPathsForActivity(Context context) {
    List<String> paths = new ArrayList<>();
    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.
                }
                paths.add(path);
            }
        }
    }
    if (paths.isEmpty()) paths.add("/storage/sdcard1");
    return paths.toArray(new String[0]);
}
 
源代码3 项目: PowerFileExplorer   文件: AndroidPathUtils.java
/**
 * Get a list of external SD card paths. (Kitkat or higher.)
 *
 * @return A list of external SD card paths.
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
private static String[] getExtSdCardPaths(final Context c) {
	List<String> paths = new ArrayList<String>();
	for (File file : c.getExternalFilesDirs("external")) {
		if (file != null && !file.equals(c.getExternalFilesDir("external"))) {
			int index = file.getAbsolutePath().lastIndexOf("/Android/data");
			if (index < 0) {
				Log.w("Uri", "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);
			}
		}
	}
	return paths.toArray(new String[paths.size()]);
}
 
源代码4 项目: PowerFileExplorer   文件: FileUtil.java
@TargetApi(Build.VERSION_CODES.KITKAT)
 public static String[] getExtSdCardPathsForActivity(Context context) {
     List<String> paths = new ArrayList<>();
     for (File file : context.getExternalFilesDirs("external")) {
         Log.d(TAG, "external: " + file.getAbsolutePath());
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.
                 }
                 paths.add(path);
             }
         }
     }
     if (paths.isEmpty()) 
paths.add("/storage/sdcard1");
     return paths.toArray(new String[0]);
 }
 
源代码5 项目: Camera-Roll-Android-App   文件: StorageUtil.java
public static File[] getRemovableStorageRoots(Context context) {
    File[] roots = context.getExternalFilesDirs("external");
    ArrayList<File> rootsArrayList = new ArrayList<>();

    for (int i = 0; i < roots.length; i++) {
        if (roots[i] != null) {
            String path = roots[i].getPath();
            int index = path.lastIndexOf("/Android/data/");
            if (index > 0) {
                path = path.substring(0, index);
                if (!path.equals(Environment.getExternalStorageDirectory().getPath())) {
                    rootsArrayList.add(new File(path));
                }
            }
        }
    }

    roots = new File[rootsArrayList.size()];
    rootsArrayList.toArray(roots);
    return roots;
}
 
源代码6 项目: commcare-android   文件: FileUtil.java
@SuppressLint("NewApi")
private static String getExternalDirectoryKitKat(Context c) {
    File[] extMounts = c.getExternalFilesDirs(null);
    // first entry is emualted storage. Second if it exists is secondary (real) SD.

    if (extMounts.length < 2) {
        return null;
    }

    /*
     * First volume returned by getExternalFilesDirs is always "primary" volume,
     * or emulated. Further entries, if they exist, will be "secondary" or external SD
     *
     * http://www.doubleencore.com/2014/03/android-external-storage/
     *
     */

    File sdRoot = extMounts[1];

    // because apparently getExternalFilesDirs entries can be null
    if (sdRoot == null) {
        return null;
    }

    return sdRoot.getAbsolutePath() + "/Android/data/org.commcare.dalvik";
}
 
源代码7 项目: leafpicrevived   文件: StorageHelper.java
public static HashSet<File> getStorageRoots(Context context) {
    HashSet<File> paths = new HashSet<File>();
    for (File file : context.getExternalFilesDirs("external")) {
        if (file != null) {
            int index = file.getAbsolutePath().lastIndexOf("/Android/data");
            if (index < 0)
                Log.w("asd", "Unexpected external file dir: " + file.getAbsolutePath());
            else
                paths.add(new File(file.getAbsolutePath().substring(0, index)));
        }
    }
    return paths;
}
 
源代码8 项目: leafpicrevived   文件: StorageHelper.java
public static String getSdcardPath(Context context) {
    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("asd", "Unexpected external file dir: " + file.getAbsolutePath());
            else
                return new File(file.getAbsolutePath().substring(0, index)).getPath();
        }
    }
    return null;
}
 
源代码9 项目: osmdroid   文件: StorageUtils.java
@SuppressLint("NewApi")
private static List<StorageInfo> getStorageListApi19(Context context) {
    ArrayList<StorageInfo> storageInfos = new ArrayList<>();

    storageInfos.add(new StorageInfo(context.getFilesDir().getAbsolutePath(), true, false, -1));

    ArrayList<File> storageDirs = new ArrayList<>();
    File[] externalDirs = context.getExternalFilesDirs( null);

    for (File externalDir : externalDirs) {
        // "Returned paths may be null if a storage device is unavailable."
        if (externalDir == null) {
            continue;
        }

        String state = Environment.getStorageState(externalDir);
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            storageDirs.add(externalDir);
        }
    }

    for (File storageDir : storageDirs) {
        storageInfos.add(new StorageInfo(storageDir.getAbsolutePath(), false, false, -1));
    }

    return storageInfos;
}
 
源代码10 项目: NClientV2   文件: Global.java
public static List<File>getUsableFolders(Context context){
    List<File>strings=new ArrayList<>();
    if(Build.VERSION.SDK_INT<Build.VERSION_CODES.Q)
        strings.add(Environment.getExternalStorageDirectory());

    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
        return strings;

    File[]files=context.getExternalFilesDirs(null);
    strings.addAll(Arrays.asList(files));
    return strings;
}
 
源代码11 项目: atlas   文件: Framework.java
public static File[] getExternalFilesDirs(Context context, String type) {
    final int version = Build.VERSION.SDK_INT;
    if (version >= 19) {
        //返回结果可能存在null值
        return context.getExternalFilesDirs(type);
    } else {
        return new File[] { context.getExternalFilesDir(type) };
    }
}
 
源代码12 项目: prevent   文件: ExternalFileUtils.java
public static File[] getExternalFilesDirs(Context context) {
    File[] files;
    files = context.getExternalFilesDirs(null);
    if (files == null) {
        files = new File[0];
    }
    return files;
}
 
源代码13 项目: microMathematics   文件: CompatUtils.java
/**
 * Procedure retrieves the storage directory
 */
public static String[] getStorageDirs(Context ctx)
{
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
    {
        File[] ff = ctx.getExternalFilesDirs(null);
        if (ff == null)
            return null;
        String[] res = new String[ff.length];
        for (int i = 0; i < ff.length; i++)
        {
            if (ff[i] == null)
                continue;
            String path = ff[i].getAbsolutePath();
            if (path == null)
                continue;
            int pos = path.indexOf("Android");
            if (pos < 0)
            {
                continue;
            }
            res[i] = path.substring(0, pos);
        }
        return res;
    }
    else
    {
        return null;
    }
}
 
源代码14 项目: AndPermission   文件: FileProvider.java
private static File[] getExternalFilesDirs(Context context, String type) {
    if (Build.VERSION.SDK_INT >= 19) {
        return context.getExternalFilesDirs(type);
    } else {
        return new File[] {context.getExternalFilesDir(type)};
    }
}
 
源代码15 项目: edx-app-android   文件: FileUtil.java
/**
 * Utility method to get the removable storage directory (such as SD-Card).
 *
 * @param context The current context.
 * @return Return removable storage directory if available otherwise null.
 */
@Nullable
public static File getRemovableStorageAppDir(@NonNull Context context) {
    final int currentApiVersion = android.os.Build.VERSION.SDK_INT;
    if (currentApiVersion >= Build.VERSION_CODES.LOLLIPOP) {
        final File[] fileList = context.getExternalFilesDirs(null);
        for (File extFile : fileList) {
            if (extFile != null && Environment.isExternalStorageRemovable(extFile)) {
                return extFile;
            }
        }
    }
    return null;
}
 
源代码16 项目: attach   文件: FileProvider.java
/**
 * Parse and return {@link PathStrategy} for given authority as defined in
 * {@link #META_DATA_FILE_PROVIDER_PATHS} {@code <meta-data>}.
 *
 * @see #getPathStrategy(Context, String)
 */
private static PathStrategy parsePathStrategy(Context context, String authority)
        throws IOException, XmlPullParserException {
    final SimplePathStrategy strat = new SimplePathStrategy(authority);

    final ProviderInfo info = context.getPackageManager()
            .resolveContentProvider(authority, PackageManager.GET_META_DATA);
    final XmlResourceParser in = info.loadXmlMetaData(
            context.getPackageManager(), META_DATA_FILE_PROVIDER_PATHS);
    if (in == null) {
        throw new IllegalArgumentException(
                "Missing " + META_DATA_FILE_PROVIDER_PATHS + " meta-data");
    }

    int type;
    while ((type = in.next()) != END_DOCUMENT) {
        if (type == START_TAG) {
            final String tag = in.getName();

            final String name = in.getAttributeValue(null, ATTR_NAME);
            String path = in.getAttributeValue(null, ATTR_PATH);

            File target = null;
            if (TAG_ROOT_PATH.equals(tag)) {
                target = DEVICE_ROOT;
            } else if (TAG_FILES_PATH.equals(tag)) {
                target = context.getFilesDir();
            } else if (TAG_CACHE_PATH.equals(tag)) {
                target = context.getCacheDir();
            } else if (TAG_EXTERNAL.equals(tag)) {
                target = Environment.getExternalStorageDirectory();
            } else if (TAG_EXTERNAL_FILES.equals(tag)) {
                File[] externalFilesDirs;
                if (Build.VERSION.SDK_INT >= 19) {
                    externalFilesDirs = context.getExternalFilesDirs(null);
                } else {
                    externalFilesDirs = new File[] { context.getExternalFilesDir(null) };
                }
                if (externalFilesDirs.length > 0) {
                    target = externalFilesDirs[0];
                }
            } else if (TAG_EXTERNAL_CACHE.equals(tag)) {
                File[] externalCacheDirs;
                if (Build.VERSION.SDK_INT >= 19) {
                    externalCacheDirs = context.getExternalCacheDirs();
                } else {
                    externalCacheDirs = new File[] { context.getExternalCacheDir() };
                }
                if (externalCacheDirs.length > 0) {
                    target = externalCacheDirs[0];
                }
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
                    && TAG_EXTERNAL_MEDIA.equals(tag)) {
                File[] externalMediaDirs = context.getExternalMediaDirs();
                if (externalMediaDirs.length > 0) {
                    target = externalMediaDirs[0];
                }
            }

            if (target != null) {
                strat.addRoot(name, buildPath(target, path));
            }
        }
    }

    return strat;
}
 
源代码17 项目: YalpStore   文件: Paths.java
static private File[] getExternalFilesDirs(Context context) {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
        ? context.getExternalFilesDirs(null)
        : new File[] {new File(Environment.getExternalStorageDirectory(), FALLBACK_DIRECTORY)}
    ;
}
 
源代码18 项目: adt-leanback-support   文件: ContextCompatKitKat.java
public static File[] getExternalFilesDirs(Context context, String type) {
    return context.getExternalFilesDirs(type);
}
 
源代码19 项目: guideshow   文件: ContextCompatKitKat.java
public static File[] getExternalFilesDirs(Context context, String type) {
    return context.getExternalFilesDirs(type);
}
 
/**
 * Returns all available SD-Cards in the system (include emulated)
 * <p/>
 * Warning: Hack! Based on Android source code of version 4.3 (API 18)
 * Because there is no standard way to get it.
 * Edited by hendrawd
 *
 * @return paths to all available SD-Cards in the system (include emulated)
 */
public static String[] getStorageDirectories(Context context) {
    // Final set of paths
    final Set<String> rv = new HashSet<>();
    // Primary physical SD-CARD (not emulated)
    final String rawExternalStorage = System.getenv("EXTERNAL_STORAGE");
    // All Secondary SD-CARDs (all exclude primary) separated by ":"
    final String rawSecondaryStoragesStr = System.getenv("SECONDARY_STORAGE");
    // Primary emulated SD-CARD
    final String rawEmulatedStorageTarget = System.getenv("EMULATED_STORAGE_TARGET");
    if (TextUtils.isEmpty(rawEmulatedStorageTarget)) {
        //fix of empty raw emulated storage on marshmallow
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            File[] files = context.getExternalFilesDirs(null);
            for (File file : files) {
                String applicationSpecificAbsolutePath = file.getAbsolutePath();
                String emulatedRootPath = applicationSpecificAbsolutePath.substring(0, applicationSpecificAbsolutePath.indexOf("Android/data"));
                rv.add(emulatedRootPath);
            }
        } else {
            // Device has physical external storage; use plain paths.
            if (TextUtils.isEmpty(rawExternalStorage)) {
                // EXTERNAL_STORAGE undefined; falling back to default.
                rv.addAll(Arrays.asList(getPhysicalPaths()));
            } else {
                rv.add(rawExternalStorage);
            }
        }
    } else {
        // Device has emulated storage; external storage paths should have
        // userId burned into them.
        final String rawUserId;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            rawUserId = "";
        } else {
            final String path = Environment.getExternalStorageDirectory().getAbsolutePath();
            final String[] folders = DIR_SEPORATOR.split(path);
            final String lastFolder = folders[folders.length - 1];
            boolean isDigit = false;
            try {
                Integer.valueOf(lastFolder);
                isDigit = true;
            } catch (NumberFormatException ignored) {
            }
            rawUserId = isDigit ? lastFolder : "";
        }
        // /storage/emulated/0[1,2,...]
        if (TextUtils.isEmpty(rawUserId)) {
            rv.add(rawEmulatedStorageTarget);
        } else {
            rv.add(rawEmulatedStorageTarget + File.separator + rawUserId);
        }
    }
    // Add all secondary storages
    if (!TextUtils.isEmpty(rawSecondaryStoragesStr)) {
        // All Secondary SD-CARDs splited into array
        final String[] rawSecondaryStorages = rawSecondaryStoragesStr.split(File.pathSeparator);
        Collections.addAll(rv, rawSecondaryStorages);
    }
    return rv.toArray(new String[rv.size()]);
}
 
 方法所在类
 同类方法