Java Code Examples for android.os.storage.StorageManager#UUID_DEFAULT

The following examples show how to use android.os.storage.StorageManager#UUID_DEFAULT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MemoryInfo.java    From MobileInfo with Apache License 2.0 6 votes vote down vote up
/**
 * API 26 android O
 * 获取总共容量大小,包括系统大小
 */
@SuppressLint("NewApi")
public static long getTotalSize(Context context, String fsUuid) {
    try {
        UUID id;
        if (fsUuid == null) {
            id = StorageManager.UUID_DEFAULT;
        } else {
            id = UUID.fromString(fsUuid);
        }
        StorageStatsManager stats = context.getSystemService(StorageStatsManager.class);
        return stats.getTotalBytes(id);
    } catch (NoSuchFieldError | NoClassDefFoundError | NullPointerException | IOException e) {
        e.printStackTrace();
        return -1;
    }
}
 
Example 2
Source File: StorageActivity.java    From AndroidDemo with MIT License 6 votes vote down vote up
/**
 * API 26 android O
 * 获取总共容量大小,包括系统大小
 */
@RequiresApi(api = 26)
public long getTotalSize(String fsUuid) {
    try {
        UUID id;
        if (fsUuid == null) {
            id = StorageManager.UUID_DEFAULT;
        } else {
            id = UUID.fromString(fsUuid);
        }
        StorageStatsManager stats = (StorageStatsManager) getSystemService(Context.STORAGE_STATS_SERVICE);
        return stats.getTotalBytes(id);
    } catch (NoSuchFieldError | NoClassDefFoundError | NullPointerException | IOException e) {
        e.printStackTrace();
        ToastUtils.show(this, "获取存储大小失败");
        return -1;
    }
}
 
Example 3
Source File: StorageQueryUtil.java    From AndroidDemo with MIT License 6 votes vote down vote up
/**
 * API 26 android O
 * 获取总共容量大小,包括系统大小
 */
@RequiresApi(Build.VERSION_CODES.O)
public static long getTotalSize(Context context, String fsUuid) {
    try {
        UUID id;
        if (fsUuid == null) {
            id = StorageManager.UUID_DEFAULT;
        } else {
            id = UUID.fromString(fsUuid);
        }
        StorageStatsManager stats = context.getSystemService(StorageStatsManager.class);
        return stats.getTotalBytes(id);
    } catch (NoSuchFieldError | NoClassDefFoundError | NullPointerException | IOException e) {
        e.printStackTrace();
        return -1;
    }
}
 
Example 4
Source File: StorageActivity.java    From AndroidDemo with MIT License 5 votes vote down vote up
/**
 * 获取存储块id
 *
 * @param fsUuid
 * @return
 */

private UUID getUuid(String fsUuid) {
    UUID id;
    if (fsUuid == null) {
        try {
            id = StorageManager.UUID_DEFAULT;
        } catch (NoSuchFieldError e) {
            id = UUID.fromString("41217664-9172-527a-b3d5-edabb50a7d69");
        }
    } else {
        id = UUID.fromString(fsUuid);
    }
    return id;
}
 
Example 5
Source File: AppSizeUtil.java    From AndroidGodEye with Apache License 2.0 5 votes vote down vote up
/**
 * 获取应用的大小
 */
@RequiresApi(api = Build.VERSION_CODES.O)
private static void getAppSizeAboveO(Context context, @NonNull OnGetSizeListener listener) {
    StorageStatsManager storageStatsManager = (StorageStatsManager) context
            .getSystemService(Context.STORAGE_STATS_SERVICE);
    StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
    // 获取所有应用的StorageVolume列表
    List<StorageVolume> storageVolumes = storageManager.getStorageVolumes();
    for (StorageVolume item : storageVolumes) {
        String uuidStr = item.getUuid();
        UUID uuid;
        if (uuidStr == null) {
            uuid = StorageManager.UUID_DEFAULT;
        } else {
            uuid = UUID.fromString(uuidStr);
        }
        int uid = getUid(context, context.getPackageName());
        // 通过包名获取uid
        StorageStats storageStats;
        try {
            storageStats = storageStatsManager.queryStatsForUid(uuid, uid);
            AppSizeInfo ctAppSizeInfo = new AppSizeInfo();
            ctAppSizeInfo.cacheSize = storageStats.getCacheBytes();
            ctAppSizeInfo.dataSize = storageStats.getDataBytes();
            ctAppSizeInfo.codeSize = storageStats.getAppBytes();
            listener.onGetSize(ctAppSizeInfo);
        } catch (IOException e) {
            listener.onError(e);
        }
    }
}