关闭

获取app文件大小 并对应响应单位:B KB MB GB; DecimalFormat

标签: DecimalFormatapp文件大小B KB MB GB
155人阅读 评论(0) 收藏 举报

 

分享:

注:参考很多资料,最后理解的基础上自己整理编写,如有冒犯,敬请见谅!谢谢

 

第一步:计算文件大小并对应相应单位

/**
	 * 文件大小计算
	 * @param fileSize
	 * @return
	 * 1B = 8b; 1KB = 1024B; 1MB = 1024KB; 1GB = 1024MB
	 */
	public String formattFileSize(int fileSize){
		DecimalFormat df = new DecimalFormat("#.00");
		String fileUnit = "";
		String woringSize = "0B";
		if(fileSize == 0){
			return woringSize;
		}
		if(fileSize < 1024){
			fileUnit = df.format((int)fileSize) + "B";
		}else if(fileSize < 1048576 && fileSize >= 1024){
			fileUnit = df.format((int)fileSize / 1024) + "KB";
		}else if(fileSize < 1073741824 && fileSize >= 1048576){
			fileUnit = df.format((int)fileSize / 1048576) + "MB";
		}else{
			fileUnit = df.format((int)fileSize / 1073741824) + "GB";
		}
		return fileUnit;
	}


二、获取appList

/**
	 * getAppList()
	 * 根据包信息,获取app
	 */ 
	private void getAppList() { 
		View mAppView = View.inflate(mContext, R.layout.car_networking_applications_listview, null);
		app_list = (ListView)mAppView.findViewById(R.id.app_list);
	
		List<PackageInfo> mPackageInfosList = getAppPackageInfoList(mContext);
		for(int i = 0; i < mPackageInfosList.size(); i++){
			PackageInfo mPackageInfo = mPackageInfosList.get(i);
			 
			mBean = new ApplicationsBean();
			mBean.setAppIcon(mPackageManager.getApplicationIcon(mPackageInfo.applicationInfo));
			mBean.setAppName(mPackageManager.getApplicationLabel(mPackageInfo.applicationInfo).toString());
			mBean.setPackgName(mPackageInfo.packageName);
			mBean.setAppVersion(mPackageInfo.versionName);
			String packgeName = mPackageInfo.applicationInfo.packageName;
			<span style="color:#ff0000;">if(mPackageInfo.packageName.equals(packgeName)){
				String dir = mPackageInfo.applicationInfo.publicSourceDir;
				int size = Integer.valueOf((int) new File(dir).length());
				String dbSize = formattFileSize(size);
</span>				mBean.setAppSize(dbSize);
 			<span style="color:#ff0000;">}
</span> 			
			mList.add(mBean);
		} 
		applicationsLists_layout.addView(mAppView);
		mAdapter =  new ApplicationAdapter(mContext, R.layout.car_networking_applications_txtview, mList);
		app_list.setAdapter(mAdapter);
	}
	


三、Adapter中

mViewHolder.txt_size1.setText(mList.get(position).getAppSize())


四、效果

 

 

 

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5461次
    • 积分:251
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    最新评论