Android獲取儲存資訊
5.1和6.0以上獲取SD卡和USB(OTG)儲存大小:
要獲取儲存裝置的儲存大小,先需要獲取裝置的路徑,我這裡的裝置SD卡都只有一張,固定為/mnt/external_sd,USB口可能有多個,網上說的一般是/mnt/usb_storage,但我找個幾個5.1的平板都獲取不到,最後通過adb shell df檢視到了usb的實際路徑並打印出所有路徑,重新寫了份獲取USB路徑的流程,整理如下:https://blog.csdn.net/l471979773/article/details/84890492
public class StorageUtil {
Context context;
private String usb_dir = null;
private String sdcard_dir = null;
private List<String> sdcard_list = null;
private List<String> usb_list = null;
String TAG ="StorageUtil" ;
public StorageUtil(Context context) {
this.context = context;
sdcard_list = new ArrayList<String>();
usb_list = new ArrayList<String>();
initStoragePath();
}
//每次獲取清除之前的資料,如果需要接收廣播實時獲取,重新呼叫此方法
private void initStoragePath() {
sdcard_list.clear();
usb_list.clear();
StorageManager mStorageManager = (StorageManager) context
.getSystemService(Context.STORAGE_SERVICE);
if (Build.VERSION.SDK_INT < 23) {
StorageVolume[] storageVolumes = mStorageManager.getVolumeList();
for (StorageVolume volume : storageVolumes) {
String externalVolumeState = mStorageManager.getVolumeState(
volume.getPath()).toString();
if (!externalVolumeState.equals(Environment.MEDIA_MOUNTED)
&& !externalVolumeState
.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
continue;
}
if (!volume.isEmulated()) {
if (volume.getPath().equals("/mnt/external_sd")) {//這裡是為了和USB形成統一,若此路徑不行,可通過adb shell df檢視新增裝置
sdcard_dir = volume.getPath();
sdcard_list.add(sdcard_dir);
} else if (volume.getPath().contains("/mnt/usb_storage")) {//USB儲存目錄,不一定是真正的USB路徑
usb_dir = volume.getPath();
File[] files = new File(usb_dir).listFiles();//adb shell df 檢視USB實際路徑,我這實際路徑在/mnt/usb_storage裡面
Log.d(TAG, "get file " + usb_dir );
for (File file : files) {
usb_dir = file.toString();
Log.d(TAG, "usb_dir = " + usb_dir );
usb_list.add(usb_dir);
}
}
}
}
return;
}else {//6.0到8.1都有這個方法,可以直接用
final List<VolumeInfo> volumes = mStorageManager.getVolumes();
Collections.sort(volumes, VolumeInfo.getDescriptionComparator());
for (VolumeInfo vol : volumes) {
if (vol.getType() == VolumeInfo.TYPE_PUBLIC) {
Log.d(TAG, "Volume path:" + vol.getPath());
DiskInfo disk = vol.getDisk();
if (disk != null) {
if (disk.isUsb()) {
// sdcard dir
StorageVolume sv = vol.buildStorageVolume(context,
context.getUserId(), false);
usb_dir = sv.getPath();
usb_list.add(usb_dir);
} else if (disk.isSd()) {
StorageVolume sv = vol.buildStorageVolume(context,
context.getUserId(), false);
sdcard_dir = sv.getPath();
sdcard_list.add(sdcard_dir);
}
}
}
}
}
}
//所有SD卡路徑
public List<String> getSdcard_list() {
return sdcard_list;
}
//所有usb路徑
public List<String> getUsb_list() {
return usb_list;
}
//SD卡的路徑
public String getSdcard_dir() {
return sdcard_dir;
}
}
知道路徑了,儲存大小也好獲取了,網上有特別多,usb和sd卡獲取大小的都一樣,我沒怎麼整理,就把程式碼貼在這:
File pathFile = new File(USB_PATH);//單獨取出usb的每個路徑
if (!pathFile.exists()) {//檔案不存在則返回
return;
}
try {
stat = new StatFs(pathFile.getPath());
} catch (Exception e) {
e.printStackTrace();
return;
}
long total = stat.getTotalBytes();//總大小
long avi = stat.getAvailableBytes();//可用大小
String totalSize = Formatter.formatFileSize(this, total);
String aviSize = Formatter.formatFileSize(this, avi);
PS:如果需要監聽插拔裝置,我這拔卡的廣播會走三次,注意清除資料。
轉載請寫明出處,若有問題可留言。https://blog.csdn.net/l471979773/article/details/84890492