public String externalSDCardPath() {
try {
StorageManager storageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
List<StorageVolume> storageVolumes = storageManager.getStorageVolumes();
Class<?> volumeClass = Class.forName("android.os.storage.StorageVolume");
Method getPath = volumeClass.getDeclaredMethod("getPath");
Method isRemovable = volumeClass.getDeclaredMethod("isRemovable");
getPath.setAccessible(true);
isRemovable.setAccessible(true);
for (int i = 0; i < storageVolumes.size(); i++) {
StorageVolume storageVolume = storageVolumes.get(i);
String mPath = (String) getPath.invoke(storageVolume);
Boolean isRemove = (Boolean) isRemovable.invoke(storageVolume);
Log.d(TAG, "Path is === " + mPath + "isRemoveble == " + isRemove);
}
}catch (Exception e){
Log.d(TAG,"error == "+e.getMessage());
}
return "";
}
private String getSDCardPath() throws Exception
{
StorageManager sm = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
Class<?> storageVolumeClass = Class.forName("android.os.storage.StorageVolume");
Method getVolumeList = sm.getClass().getMethod("getVolumeList");
Method getPath = storageVolumeClass.getMethod("getPath");
Method isRemovable = storageVolumeClass.getMethod("isRemovable");
Object result = getVolumeList.invoke(sm);
for(int i = 0; i < Array.getLength(result); ++i)
{
Object element = Array.get(result, i);
boolean removable = (Boolean) isRemovable.invoke(element);
if(removable)
{
return (String) getPath.invoke(element);
}
}
throw new RuntimeException("no suitable SD path found");
}
private StorageManager mStorageManager;
private StorageVolume[] mStorageVolumes;
private File mSdcard;
private File mEmmc;
private File mUsbStorage;
private void updateStoragePaths() {
if (mStorageManager == null) {
mStorageManager = (StorageManager) getApplicationContext().getSystemService(Context.STORAGE_SERVICE);
try {
Method method = mStorageManager.getClass().getMethod("getVolumeList");
mStorageVolumes = (StorageVolume[]) method.invoke(mStorageManager);
for (int i = 0; i < mStorageVolumes.length; i++) {
Method method2 = mStorageVolumes[i].getClass().getMethod("getPathFile");
File storage = (File) method2.invoke(mStorageVolumes[i]);
switch (i) {
case 0:
mEmmc = storage;
break;
case 1:
mSdcard = storage;
long totalSpace = mSdcard.getTotalSpace();
long freeSpace = mSdcard.getFreeSpace();
long usableSpace = mSdcard.getUsableSpace();
String totalSpaceStr = Formatter.formatFileSize(this, totalSpace);
String freeSpaceStr = Formatter.formatFileSize(this, freeSpace);
String usableSpaceStr = Formatter.formatFileSize(this, usableSpace);
Log.d(TAG,"totalSpaceStr == "+totalSpaceStr);
Log.d(TAG,"freeSpaceStr == "+freeSpaceStr);
Log.d(TAG,"usableSpaceStr == "+usableSpaceStr);
break;
case 2:
mUsbStorage = storage;
break;
}
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
sd卡路径
最新推荐文章于 2024-07-16 17:10:08 发布