下面列出了android.os.Environment#getStorageState ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private String getStorageState(File path) {
if(path.getAbsolutePath().startsWith(KernalConstants.baseContext.getFilesDir().getAbsolutePath())){
return MEDIA_MOUNTED;
}
final int version = Build.VERSION.SDK_INT;
if (version >= 19) {
return Environment.getStorageState(path);
}
try {
final String canonicalPath = path.getCanonicalPath();
final String canonicalExternal = Environment.getExternalStorageDirectory()
.getCanonicalPath();
if (canonicalPath.startsWith(canonicalExternal)) {
return Environment.getExternalStorageState();
}
} catch (IOException e) {
}
return MEDIA_UNKNOWN;
}
public static String getStorageState(File path) {
final int version = Build.VERSION.SDK_INT;
if (version >= 19) {
return Environment.getStorageState(path);
}
try {
final String canonicalPath = path.getCanonicalPath();
final String canonicalExternal = Environment.getExternalStorageDirectory()
.getCanonicalPath();
if (canonicalPath.startsWith(canonicalExternal)) {
return Environment.getExternalStorageState();
}
} catch (IOException e) {
}
return MEDIA_UNKNOWN;
}
@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;
}
@TargetApi(Build.VERSION_CODES.KITKAT)
private void updateVolumesLocked() {
mRoots.clear();
mIdToPath.clear();
mIdToRoot.clear();
int count = 0;
StorageUtils storageUtils = new StorageUtils(getContext());
for (StorageVolume volume : storageUtils.getStorageMounts()) {
final File path = volume.getPathFile();
if(Utils.hasKitKat()){
String state = Environment.getStorageState(path);
final boolean mounted = Environment.MEDIA_MOUNTED.equals(state)
|| Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
if (!mounted) continue;
}
final String rootId;
if (volume.isPrimary() && volume.isEmulated()) {
rootId = ROOT_ID_PRIMARY_EMULATED;
} else if (volume.getUuid() != null) {
rootId = ROOT_ID_SECONDARY + volume.getUserLabel();
} else {
Log.d(TAG, "Missing UUID for " + volume.getPath() + "; skipping");
continue;
}
if (mIdToPath.containsKey(rootId)) {
Log.w(TAG, "Duplicate UUID " + rootId + "; skipping");
continue;
}
try {
if(null == path.listFiles()){
continue;
}
mIdToPath.put(rootId, path);
final RootInfo root = new RootInfo();
root.rootId = rootId;
root.flags = Document.FLAG_SUPPORTS_THUMBNAIL;
if (ROOT_ID_PRIMARY_EMULATED.equals(rootId)) {
root.title = getContext().getString(R.string.root_internal_storage);
} else {
count++;
root.title = getContext().getString(R.string.root_external_storage) + " " + count;// + volume.getLabel();
}
root.docId = getDocIdForFile(path);
mRoots.add(root);
mIdToRoot.put(rootId, root);
} catch (FileNotFoundException e) {
throw new IllegalStateException(e);
}
}
Log.d(TAG, "After updating volumes, found " + mRoots.size() + " active roots");
getContext().getContentResolver()
.notifyChange(DocumentsContract.buildRootsUri(AUTHORITY), null, false);
}
@TargetApi(Build.VERSION_CODES.KITKAT)
private void updateVolumesLocked() {
mRoots.clear();
mIdToPath.clear();
mIdToRoot.clear();
int count = 0;
StorageUtils storageUtils = new StorageUtils(getContext());
for (StorageVolume volume : storageUtils.getStorageMounts()) {
final File path = volume.getPathFile();
if(Utils.hasKitKat()){
String state = Environment.getStorageState(path);
final boolean mounted = Environment.MEDIA_MOUNTED.equals(state)
|| Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
if (!mounted) continue;
}
final String rootId;
if (volume.isPrimary() && volume.isEmulated()) {
rootId = ROOT_ID_PRIMARY_EMULATED;
} else if (volume.getUuid() != null) {
rootId = ROOT_ID_SECONDARY + volume.getUserLabel();
} else {
Log.d(TAG, "Missing UUID for " + volume.getPath() + "; skipping");
continue;
}
if (mIdToPath.containsKey(rootId)) {
Log.w(TAG, "Duplicate UUID " + rootId + "; skipping");
continue;
}
try {
if(null == path.listFiles()){
continue;
}
mIdToPath.put(rootId, path);
final RootInfo root = new RootInfo();
root.rootId = rootId;
root.flags = Document.FLAG_SUPPORTS_THUMBNAIL;
if (ROOT_ID_PRIMARY_EMULATED.equals(rootId)) {
root.title = getContext().getString(R.string.root_internal_storage);
} else {
count++;
root.title = getContext().getString(R.string.root_external_storage) + " " + count;// + volume.getLabel();
}
root.docId = getDocIdForFile(path);
mRoots.add(root);
mIdToRoot.put(rootId, root);
} catch (FileNotFoundException e) {
throw new IllegalStateException(e);
}
}
Log.d(TAG, "After updating volumes, found " + mRoots.size() + " active roots");
getContext().getContentResolver()
.notifyChange(DocumentsContract.buildRootsUri(AUTHORITY), null, false);
}
@TargetApi(Build.VERSION_CODES.KITKAT)
private void updateVolumesLocked() {
mRoots.clear();
mIdToPath.clear();
mIdToRoot.clear();
int count = 0;
StorageUtils storageUtils = new StorageUtils(getContext());
for (StorageVolume volume : storageUtils.getStorageMounts()) {
final File path = volume.getPathFile();
if(Utils.hasKitKat()){
String state = Environment.getStorageState(path);
final boolean mounted = Environment.MEDIA_MOUNTED.equals(state)
|| Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
if (!mounted) continue;
}
final String rootId;
if (volume.isPrimary() && volume.isEmulated()) {
rootId = ROOT_ID_PRIMARY_EMULATED;
} else if (volume.getUuid() != null) {
rootId = ROOT_ID_SECONDARY + volume.getUserLabel();
} else {
Log.d(TAG, "Missing UUID for " + volume.getPath() + "; skipping");
continue;
}
if (mIdToPath.containsKey(rootId)) {
Log.w(TAG, "Duplicate UUID " + rootId + "; skipping");
continue;
}
try {
if(null == path.listFiles()){
continue;
}
mIdToPath.put(rootId, path);
final RootInfo root = new RootInfo();
root.rootId = rootId;
root.flags = Document.FLAG_SUPPORTS_THUMBNAIL;
if (ROOT_ID_PRIMARY_EMULATED.equals(rootId)) {
root.title = getContext().getString(R.string.root_internal_storage);
} else {
count++;
root.title = getContext().getString(R.string.root_external_storage) + " " + count;// + volume.getLabel();
}
root.docId = getDocIdForFile(path);
mRoots.add(root);
mIdToRoot.put(rootId, root);
} catch (FileNotFoundException e) {
throw new IllegalStateException(e);
}
}
Log.d(TAG, "After updating volumes, found " + mRoots.size() + " active roots");
getContext().getContentResolver()
.notifyChange(DocumentsContract.buildRootsUri(AUTHORITY), null, false);
}
public static String getStorageState(File path) {
return Environment.getStorageState(path);
}
public static String getStorageState(File path) {
return Environment.getStorageState(path);
}
public static String getStorageState(File path) {
return Environment.getStorageState(path);
}