80,349
社区成员
发帖
与我相关
我的任务
分享
File file=new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), "12222.apk");
file.createNewFile();
// 获取次存储卡路径,一般就是外置 TF 卡了. 不过也有可能是 USB OTG 设备...
// 其实只要判断第二张卡在挂载状态,就可以用了.
public String getSecondaryStoragePath() {
try {
StorageManager sm = (StorageManager) getSystemService(STORAGE_SERVICE);
Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths", null);
String[] paths = (String[]) getVolumePathsMethod.invoke(sm, null);
// second element in paths[] is secondary storage path
return paths.length <= 1 ? null : paths[1];
} catch (Exception e) {
Log.e(TAG, "getSecondaryStoragePath() failed", e);
}
return null;
}
// 获取存储卡的挂载状态. path 参数传入上方法得到的路径
public String getStorageState(String path) {
try {
StorageManager sm = (StorageManager) getSystemService(STORAGE_SERVICE);
Method getVolumeStateMethod = StorageManager.class.getMethod("getVolumeState", new Class[]{String.class});
String state = (String) getVolumeStateMethod.invoke(sm, path);
return state;
} catch (Exception e) {
Log.e(TAG, "getStorageState() failed", e);
}
return null;
}