Last active
August 29, 2015 14:13
-
-
Save easternHong/5acbd86c257642503c0a to your computer and use it in GitHub Desktop.
Android External Internal path获取
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cylan.efamily.utils; | |
import android.annotation.SuppressLint; | |
import android.os.Environment; | |
import android.os.StatFs; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* internal = getExtSDCardPaths().get(0);//always good | |
* external = getExtSDCardPaths().size()>0?getExtSDCardPaths().get(1):null; | |
*/ | |
public class SdCardUtils { | |
private final static int GB_2_BYTE = 1073741824; | |
private final static int MB_2_BYTE = 1048576; | |
public static long getGUnit() { | |
return GB_2_BYTE; | |
} | |
public static long getMUnit() { | |
return MB_2_BYTE; | |
} | |
/** | |
* @param path: internal path or external path | |
* @return | |
*/ | |
public static double getStorageSize(String path) { | |
System.out.println("path: " + path); | |
StatFs sf = new StatFs(path); | |
return ((double) sf.getBlockCount() * sf.getBlockSize()) / getGUnit(); | |
} | |
/** | |
* @return True if the external storage is available. False otherwise. | |
*/ | |
public static boolean isAvailable() { | |
String state = Environment.getExternalStorageState(); | |
if (Environment.MEDIA_MOUNTED.equals(state) | |
|| Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { | |
return true; | |
} | |
return false; | |
} | |
/** | |
* @return True if the external storage is writable. False otherwise. | |
*/ | |
public static boolean isWritable() { | |
String state = Environment.getExternalStorageState(); | |
if (Environment.MEDIA_MOUNTED.equals(state)) { | |
return true; | |
} | |
return false; | |
} | |
/** | |
* only flag external sdcard | |
* | |
* @return | |
*/ | |
@SuppressLint("NewApi") | |
public static boolean externalSDCardExist() { | |
return Environment.isExternalStorageRemovable(); | |
} | |
/** | |
* <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> | |
* <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> | |
* | |
* @return List<String> | |
* @throws IOException | |
* @Title: getExtSDCardPaths | |
* @Description: to obtain storage paths, the first path is theoretically | |
* the returned value of | |
* Environment.getExternalStorageDirectory(), namely the | |
* primary external storage. It can be the storage of internal | |
* device, or that of external sdcard. If paths.size() >1, | |
* basically, the current device contains two type of storage: | |
* one is the storage of the device itself, one is that of | |
* external sdcard. Additionally, the paths is directory. | |
*/ | |
public static List<String> getExtSDCardPaths() { | |
List<String> paths = new ArrayList<String>(); | |
String extFileStatus = Environment.getExternalStorageState(); | |
File extFile = Environment.getExternalStorageDirectory(); | |
if (extFileStatus.equals(Environment.MEDIA_MOUNTED) && extFile.exists() | |
&& extFile.isDirectory() && extFile.canWrite()) { | |
paths.add(extFile.getAbsolutePath()); | |
} | |
try { | |
// obtain executed result of command line code of 'mount', to | |
// judge | |
// whether tfCard exists by the result | |
Runtime runtime = Runtime.getRuntime(); | |
Process process = runtime.exec("mount"); | |
InputStream is = process.getInputStream(); | |
InputStreamReader isr = new InputStreamReader(is); | |
BufferedReader br = new BufferedReader(isr); | |
String line = null; | |
int mountPathIndex = 1; | |
while ((line = br.readLine()) != null) { | |
// format of sdcard file system: vfat/fuse | |
if ((!line.contains("fat") && !line.contains("fuse") && !line | |
.contains("storage")) | |
|| line.contains("secure") | |
|| line.contains("asec") | |
|| line.contains("firmware") | |
|| line.contains("shell") | |
|| line.contains("obb") | |
|| line.contains("legacy") || line.contains("data")) { | |
continue; | |
} | |
String[] parts = line.split(" "); | |
int length = parts.length; | |
if (mountPathIndex >= length) { | |
continue; | |
} | |
String mountPath = parts[mountPathIndex]; | |
if (!mountPath.contains("/") || mountPath.contains("data") | |
|| mountPath.contains("Data")) { | |
continue; | |
} | |
File mountRoot = new File(mountPath); | |
if (!mountRoot.exists() || !mountRoot.isDirectory() | |
|| !mountRoot.canWrite()) { | |
continue; | |
} | |
boolean equalsToPrimarySD = mountPath.equals(extFile | |
.getAbsolutePath()); | |
if (equalsToPrimarySD) { | |
continue; | |
} | |
paths.add(mountPath); | |
} | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return paths; | |
} | |
/** | |
* @return: external path | |
*/ | |
public static String getExternalPath() { | |
List<String> list = getExtSDCardPaths(); | |
if (list != null && list.size() > 1) | |
return list.get(1); | |
return null; | |
} | |
/** | |
* @return: internal path | |
*/ | |
public static String getInternalPath() { | |
return getExtSDCardPaths().get(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment