Androidはストレージファイルのすべてのパスを取得


引用:みんなはappの开発をする时、基本的にすべてファイルを携帯电话に保存して、androidはファイルを保存する地方が多くて、iosのように、ファイルを今のappのディレクトリの下に保存するしかなくて、しかもandroid携帯电话はメーカーがromをカスタマイズしたため、sdcardの経路は异なるハンドマシンの上ですべて异なります.私はパスを取得するいくつかの方法をカプセル化して、1つのツールクラスに入れました.
1.拡張記憶装置2を取得する.sdcard 2の外部記憶領域3を取得する.利用可能なEMMC内部記憶領域4を取得する.他の外部記憶領域5を取得する.内部ストレージディレクトリの取得
Activityプログラムのエントリは、oncreateメソッドでツールクラスを介してファイル保存パスを取得し、印刷する.(指定したサイズの空のファイルを作成する方法も書かれており、必要に応じて呼び出すことができます)
/**
 *       ,      
 * @author ansen
 * @create time 2015-09-07
 */
public class MainActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		String str=FileUtil.getCachePath();
//		writeFileSize(str+"/ansen.mp3",50);  //        ansen.mp3        50 
		System.out.println(str);
	}
	
	/**
	 *          .     
	 * @param filePath      
	 * @param size            
	 */
	private void writeFileSize(String filePath,int size){
		try {
			RandomAccessFile raf = new RandomAccessFile(filePath,"rw");
			raf.seek(raf.length());//         
			for (int i = 0; i < size; i++) {//    260 ,              
			    byte[] buffer = new byte[1024*1024]; //1 1M,         ,      。
			    raf.write(buffer);
			    System.out.println("  1 ..."+i);
			}
			raf.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

ファイルツール類は、共通の方法でカプセル化する、ファイルの保存経路を取得し、合計5つの経路を取得し、5つの経路の予約空間が50メガより大きいか否かを順次判断することができる.より大きい場合はパスを直接返します
/**
 *      
 * @author ansen
 * @create time 2015-09-07
 */
public final class FileUtil {
	private static final String FOLDER_NAME = "ansen";//        app  
	private static final long MIN_STORAGE=52428800;//50*1024*1024  50m
	
	//    
	public static String getCachePath(){
		String path = getSavePath(MIN_STORAGE);
		if(TextUtils.isEmpty(path)){
			return null;
		}
		path= path + FOLDER_NAME + "/cache";
		makeDir(path);
		return path;
	}
	
	/**
	 *           
	 * @param saveSize      
	 * @return     
	 */
	private static String getSavePath(long saveSize) {
	    String savePath = null;
	    if (StorageUtil.getExternaltStorageAvailableSpace() > saveSize) {//      >    
	        savePath = StorageUtil.getExternalStorageDirectory();
	        File saveFile = new File(savePath);
	        if (!saveFile.exists()) {
	            saveFile.mkdirs();
	        } else if (!saveFile.isDirectory()) {
	            saveFile.delete();
	            saveFile.mkdirs();
	        }
	    } else if (StorageUtil.getSdcard2StorageAvailableSpace() > saveSize) {//sdcard2      >    
	        savePath = StorageUtil.getSdcard2StorageDirectory();
	        File saveFile = new File(savePath);
	        if (!saveFile.exists()) {
	            saveFile.mkdirs();
	        } else if (!saveFile.isDirectory()) {
	            saveFile.delete();
	            saveFile.mkdirs();
	        }
	    } else if (StorageUtil.getEmmcStorageAvailableSpace() > saveSize) {//    EMMC       >    
	        savePath = StorageUtil.getEmmcStorageDirectory();
	        File saveFile = new File(savePath);
	        if (!saveFile.exists()) {
	            saveFile.mkdirs();
	        } else if (!saveFile.isDirectory()) {
	            saveFile.delete();
	            saveFile.mkdirs();
	        }
	    } else if (StorageUtil.getOtherExternaltStorageAvailableSpace()>saveSize) {//          >    
	        savePath = StorageUtil.getOtherExternalStorageDirectory();
	        File saveFile = new File(savePath);
	        if (!saveFile.exists()) {
	            saveFile.mkdirs();
	        } else if (!saveFile.isDirectory()) {
	            saveFile.delete();
	            saveFile.mkdirs();
	        }
	    }else if (StorageUtil.getInternalStorageAvailableSpace() > saveSize) {//      >    
	        savePath = StorageUtil.getInternalStorageDirectory() + File.separator;
	    }
	    return savePath;
	}
	
	/**
	 *      
	 * @param path
	 */
	private static void makeDir(String path){
		File file = new File(path);
		if(!file.exists()){
			file.mkdirs();
		}
		file = null;
	}
}

FileUtilクラスの呼び出しのために、様々なパスを取得するいくつかの方法がカプセル化されている.
/**
 *               
 * @author ansen
 * @create time 2015-09-07
 */
@SuppressLint("NewApi")
public final class StorageUtil {
	private static String otherExternalStorageDirectory = null;
	private static int kOtherExternalStorageStateUnknow = -1;
	private static int kOtherExternalStorageStateUnable = 0;
	private static int kOtherExternalStorageStateIdle = 1;
	private static int otherExternalStorageState = kOtherExternalStorageStateUnknow;
	private static String internalStorageDirectory;
	
	public static Context context;

	public static void init(Context cxt) {
		context = cxt;
	}
	
	/** get external Storage available space */
	public static long getExternaltStorageAvailableSpace() {
		if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			return 0;
		}
		File path = Environment.getExternalStorageDirectory();
		StatFs statfs = new StatFs(path.getPath());
		
		long blockSize;
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
			blockSize = statfs.getBlockSizeLong();
		}else {
			blockSize = statfs.getBlockSize();
		}

		long availableBlocks;
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
			availableBlocks = statfs.getAvailableBlocksLong();
		}else {
			availableBlocks = statfs.getAvailableBlocks();
		}
		return blockSize * availableBlocks;
	}
	
	public final static String getInternalStorageDirectory() {
		if (TextUtils.isEmpty(internalStorageDirectory)) {
			File file = context.getFilesDir();
			internalStorageDirectory = file.getAbsolutePath();
			if (!file.exists())
				file.mkdirs();
			String shellScript = "chmod 705 " + internalStorageDirectory;
			runShellScriptForWait(shellScript);
		}
		return internalStorageDirectory;
	}
	
	public static long getInternalStorageAvailableSpace() {
		String path = getInternalStorageDirectory();
		StatFs stat = new StatFs(path);
//		long blockSize = stat.getBlockSizeLong();
		long blockSize;
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
			blockSize = stat.getBlockSizeLong();
		}else {
			blockSize = stat.getBlockSize();
		}
//		long availableBlocks = stat.getAvailableBlocksLong();
		long availableBlocks;
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
			availableBlocks = stat.getAvailableBlocksLong();
		}else {
			availableBlocks = stat.getAvailableBlocks();
		}
		
		return blockSize * availableBlocks;
	}
	
	
	
	public final static String getExternalStorageDirectory() {
		return Environment.getExternalStorageDirectory() + File.separator + "";
	}
	
	/** get sdcard2 external Storage available space */
	public static long getSdcard2StorageAvailableSpace() {
		if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			return 0;
		}
		String path = getSdcard2StorageDirectory();
		File file = new File(path);
		if (!file.exists())
			return 0;
		StatFs statfs = new StatFs(path);
//		long blockSize = statfs.getBlockSizeLong();
		long blockSize;
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
			blockSize = statfs.getBlockSizeLong();
		}else {
			blockSize = statfs.getBlockSize();
		}
		
//		long availableBlocks = statfs.getAvailableBlocksLong();
		long availableBlocks;
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
			availableBlocks = statfs.getAvailableBlocksLong();
		}else {
			availableBlocks = statfs.getAvailableBlocks();
		}
		
		return blockSize * availableBlocks;
	}
	
	public final static String getSdcard2StorageDirectory() {
		return "/mnt/sdcard2/";
	}
	
	public static boolean runShellScriptForWait(final String cmd)throws SecurityException {
		ShellThread thread = new ShellThread(cmd);
		thread.setDaemon(true);
		thread.start();
		int k = 0;
		while (!thread.isReturn() && k++ < 20) {
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (k >= 20) {
			thread.interrupt();
		}
		return thread.isSuccess();
	}
	
	/**     shell      */
	private static class ShellThread extends Thread {
		private boolean isReturn;
		private boolean isSuccess;
		private String cmd;

		public boolean isReturn() {
			return isReturn;
		}

		public boolean isSuccess() {
			return isSuccess;
		}

		/**
		 * @param cmd shell    
		 * @param isReturn          
		 * @param isSuccess Process      
		 */
		public ShellThread(String cmd) {
			this.cmd = cmd;
		}

		@Override
		public void run() {
			try {
				Runtime runtime = Runtime.getRuntime();
				Process proc;
				try {
					proc = runtime.exec(cmd);
					isSuccess = (proc.waitFor() == 0);
				} catch (IOException e) {
					e.printStackTrace();
				}
				isSuccess = true;
			} catch (InterruptedException e) {
			}
			isReturn = true;
		}
	}
	
	/** get EMMC internal Storage available space */
	public static long getEmmcStorageAvailableSpace() {
		String path = getEmmcStorageDirectory();
		File file = new File(path);
		if (!file.exists())
			return 0;
		StatFs statfs = new StatFs(path);
//		long blockSize = statfs.getBlockSizeLong();
		long blockSize;
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
			blockSize = statfs.getBlockSizeLong();
		}else {
			blockSize = statfs.getBlockSize();
		}
		
//		long availableBlocks = statfs.getAvailableBlocksLong();
		long availableBlocks;
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
			availableBlocks = statfs.getAvailableBlocksLong();
		}else {
			availableBlocks = statfs.getAvailableBlocks();
		}

		return blockSize * availableBlocks;
	}
	
	public final static String getEmmcStorageDirectory() {
		return "/mnt/emmc/";
	}
	
	/** get other external Storage available space */
	public static long getOtherExternaltStorageAvailableSpace() {
		if (!Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			return 0;
		}
		if (otherExternalStorageState == kOtherExternalStorageStateUnable)
			return 0;
		if (otherExternalStorageDirectory == null) {
			getOtherExternalStorageDirectory();
		}
		if (otherExternalStorageDirectory == null)
			return 0;
		StatFs statfs = new StatFs(otherExternalStorageDirectory);
//		long blockSize = statfs.getBlockSizeLong();
		long blockSize;
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
			blockSize = statfs.getBlockSizeLong();
		}else {
			blockSize = statfs.getBlockSize();
		}
//		long availableBlocks = statfs.getAvailableBlocksLong();
		long availableBlocks;
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
			availableBlocks = statfs.getAvailableBlocksLong();
		}else {
			availableBlocks = statfs.getAvailableBlocks();
		}
		return blockSize * availableBlocks;
	}
	
	
	public static String getOtherExternalStorageDirectory() {
		if (otherExternalStorageState == kOtherExternalStorageStateUnable)
			return null;
		if (otherExternalStorageState == kOtherExternalStorageStateUnknow) {
			FstabReader fsReader = new FstabReader();
			if (fsReader.size() <= 0) {
				otherExternalStorageState = kOtherExternalStorageStateUnable;
				return null;
			}
			List<StorageInfo> storages = fsReader.getStorages();
			/*         100M         */
			long availableSpace = 100 << (20);
			String path = null;
			for (int i = 0; i < storages.size(); i++) {
				StorageInfo info = storages.get(i);
				if (info.getAvailableSpace() > availableSpace) {
					availableSpace = info.getAvailableSpace();
					path = info.getPath();
				}
			}
			otherExternalStorageDirectory = path;
			if (otherExternalStorageDirectory != null) {
				otherExternalStorageState = kOtherExternalStorageStateIdle;
			} else {
				otherExternalStorageState = kOtherExternalStorageStateUnable;
			}
			if(!TextUtils.isEmpty(otherExternalStorageDirectory)){
				if(!otherExternalStorageDirectory.endsWith("/")){
					otherExternalStorageDirectory=otherExternalStorageDirectory+"/";
				}
			}
		}
		return otherExternalStorageDirectory;
	}
	
	public static class FstabReader {
		public FstabReader() {
			init();
		}

		public int size() {
			return storages == null ? 0 : storages.size();
		}

		public List<StorageInfo> getStorages() {
			return storages;
		}

		final List<StorageInfo> storages = new ArrayList<StorageInfo>();

		public void init() {
			File file = new File("/system/etc/vold.fstab");
			if (file.exists()) {
				FileReader fr = null;
				BufferedReader br = null;
				try {
					fr = new FileReader(file);
					if (fr != null) {
						br = new BufferedReader(fr);
						String s = br.readLine();
						while (s != null) {
							if (s.startsWith("dev_mount")) {
								/* "\s"         : /     */
								String[] tokens = s.split("\\s");
								String path = tokens[2]; // mount_point
								StatFs stat = new StatFs(path);
								
								long blockSize;
								long totalBlocks;
								long availableBlocks;
								if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
									blockSize = stat.getBlockSizeLong();
								}else {
									blockSize = stat.getBlockSize();
								}
								if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
									totalBlocks = stat.getBlockCountLong();
								}else {
									totalBlocks = stat.getBlockCount();
								}
								if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
									availableBlocks = stat.getAvailableBlocksLong();
								}else {
									availableBlocks = stat.getAvailableBlocks();
								}
								
//								if (null != stat&& stat.getAvailableBlocksLong() > 0) {
//
//									long availableSpace = stat.getAvailableBlocksLong()* stat.getBlockSizeLong();
//									long totalSpace = stat.getBlockCountLong()* stat.getBlockSizeLong();
								if (null != stat&& availableBlocks > 0) {

									long availableSpace = availableBlocks* blockSize;
									long totalSpace = totalBlocks* blockSize;
									StorageInfo storage = new StorageInfo(path,
											availableSpace, totalSpace);
									storages.add(storage);
								}
							}
							s = br.readLine();
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					if (fr != null)
						try {
							fr.close();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					if (br != null)
						try {
							br.close();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
				}
			}
		}
	}

	static class StorageInfo implements Comparable<StorageInfo> {
		private String path;
		private long availableSpace;
		private long totalSpace;

		StorageInfo(String path, long availableSpace, long totalSpace) {
			this.path = path;
			this.availableSpace = availableSpace;
			this.totalSpace = totalSpace;
		}

		@Override
		public int compareTo(StorageInfo another) {
			if (null == another)
				return 1;

			return this.totalSpace - another.totalSpace > 0 ? 1 : -1;
		}

		long getAvailableSpace() {
			return availableSpace;
		}

		long getTotalSpace() {
			return totalSpace;
		}

		String getPath() {
			return path;
		}
	}
	
}

最後にAndroidManifestで覚えています.xmlでの読み書きsdcard権限の構成

クリックしてソースをダウンロード