zoukankan      html  css  js  c++  java
  • 安卓SD卡的操作

    /**
    	 * 得到常用路径
    	 */
    	public void getDir(){
    		// /mnt/sdcard
    		File root=Environment.getExternalStorageDirectory();
    		
    		// /system
    		File root=Environment.getRootDirectory();
    		
    		// /cache
    		File root=Environment.getDownloadCacheDirectory();
    		
    		// /data
    		File root=Environment.getDataDirectory();
    		
    		
    		// /storage/emulated/0/Music
    		//String type=Environment.DIRECTORY_MUSIC;
    		
    		//String type=Environment.DIRECTORY_PICTURES;
    		File root=Environment.getExternalStoragePublicDirectory(type);
    		
    		
    		System.out.println("root="+root.getAbsolutePath());
    	}
    	
    	
    	/**
    	 * 获取手机SDCard的总容量
    	 * @return
    	 */
    	public long getSDCardTotalCapacity(){
    		if(this.isSDCardUse()){
    			// /mnt/sdcard
    			File root=Environment.getExternalStorageDirectory();
    			//根据SDCard的路径得到状态文件系统对象
    			StatFs statFs=new StatFs(root.getPath());
    			
    			//得到每一块数据的大小,以字节为单位
    			long blockSize=statFs.getBlockSize();
    			System.out.println("blockSize="+blockSize);
    			
    			//得到当前手机SDCard一共有多少个数据块
    			long blockCount=statFs.getBlockCount();
    			System.out.println("blockCount="+blockCount);
    			
    			//return blockCount*blockSize;//默认以字节为单位
    			return blockCount*blockSize/1024/1024/1024;//默认以字节为单位,换算成GB
    			
    		}else{
    			throw new RuntimeException("当前手机SDCard不存在或者不可用!");
    		}
    	}
    	
    	/**
    	 * 获取手机SDCard可用容量
    	 * @return
    	 */
    	public long getSDCardAvailableCapacity(){
    		if(this.isSDCardUse()){
    			// /mnt/sdcard
    			File root=Environment.getExternalStorageDirectory();
    			//根据SDCard的路径得到状态文件系统对象
    			StatFs statFs=new StatFs(root.getAbsolutePath());
    			
    			//得到每一块数据的大小,以字节为单位
    			int blockSize=statFs.getBlockSize();
    			
    			//得到当前手机SDCard可用容量的SDCard块数
    			int availableBlocks=statFs.getAvailableBlocks();
    			
    			
    			//return availableBlocks*blockSize;//默认以字节为单位
    			return availableBlocks*blockSize/1024/1024/1024;//默认以字节为单位,换算成GB
    			
    		}else{
    			throw new RuntimeException("当前手机SDCard不存在或者不可用!");
    		}
    	}
    	
    	/**
    	 * 获取手机SDCard剩余容量
    	 * @return
    	 */
    	public long getSDCardFreeCapacity(){
    		if(this.isSDCardUse()){
    			// /mnt/sdcard
    			File root=Environment.getExternalStorageDirectory();
    			//根据SDCard的路径得到状态文件系统对象
    			StatFs statFs=new StatFs(root.getAbsolutePath());
    			
    			//得到每一块数据的大小,以字节为单位
    			int blockSize=statFs.getBlockSize();
    			
    			//得到当前手机SDCard可用容量的SDCard块数
    			int freeBlocks=statFs.getFreeBlocks();
    			
    			
    			//return freeBlocks*blockSize;//默认以字节为单位
    			return freeBlocks*blockSize/1024/1024/1024;//默认以字节为单位,换算成MB
    			
    		}else{
    			throw new RuntimeException("当前手机SDCard不存在或者不可用!");
    		}
    	}
    }
    

      

  • 相关阅读:
    [USACO4.2]草地排水Drainage Ditches
    bzoj3236:[AHOI2013]作业
    小A买彩票-(组合数)
    CSS样式整理大全
    P1880 [NOI1995]石子合并-(环形区间dp)
    P1147连续自然数和-(尺取法)
    POJ2456Aggressive cows-(二分判定)
    NYOJ737石子合并(二)-(区间dp)
    牛客网-乌龟跑步-(dfs)
    int和string之间的转换
  • 原文地址:https://www.cnblogs.com/qcgAd/p/5091178.html
Copyright © 2011-2022 走看看