关于SD卡的

参考了 :http://www.cnblogs.com/mengdd/p/3742623.html  ,http://blog.csdn.net/fdd11119/article/details/51500670 

   http://www.cnblogs.com/ywq-come/p/5925654.html

  了解到关于手机sd 卡的一些信息 ,总结一下。


 1:我们获取外部SD卡信息,调用的是 Environment.getExternalStorageDirectory() 这个方法。但是调用这个方法,有的时候会出现不同的情况。

先介绍一种我们以前知道的东西,“TF卡,也就是我们以前所认为的SD卡。以前我们管他叫存储卡,手机内存不够用的时候买的那种玩意。


我们现在所说的内部存储,其实以前是外置SD卡,即在Android出世的前几年,那时手机的存储是十分有限的,不像现在到处可见16G、32G和64G的存储,因而那时候的手机有的厂商允许插入外置的SD卡,此时这张卡仍处于手机的扩展部分。后来,随着手机的发展以及存储能力的增加,这张外置SD卡,逐渐成为了手机的一部分,不再允许可插拔了。所以,在手机中虽然是描述说内部存储,他其实是外置的存储卡中嵌入到手机中而已。方法名中的 external 也是外部的意思。


http://www.upantool.com/sense/2015/6945.html   这里描述了SD卡和TF卡的区别。

如果手机支持TF卡扩展,并且本身没有大容量存储,那么Environment.getExternalStorageDirectory() 获取到的 就是我们插入的TF卡的的路径。

如果手机本身有大容量存储,不论手机是否支持TF卡扩展, Environment.getExternalStorageDirectory() 获取到的是我们认为的内部储存的路径,也就是说,如果手机本身具有大容量存储,又支持TF卡扩展,通过这条命令是无法获取到TF卡信息的。

如下图中,手机存在内部和SD卡两个存储设备 ,但是我们通过Environment.getExternalStorageDirectory() 这个方法,获取到的路径却是内部存储设备的路径。






2.程序文件

不建议将需要存储的文件直接放在获取到的存储设备的根目录下,因为那样会使我们的APP看上去非常的混乱,我们有几个目录是跟随我们的APP而存在的,当我们卸载我们的APP之后,这些目录下的文件也会随着删除掉,
它们有两个地方:internal storage 和 external storage 。 internal storage是我们正在意义上的内部存储, 就是手机自带的一块存储区域 。而 external storage 就是我们的内部存储设备,也就是SD卡。
internal storage 是一个私有的目录,shared preference文件,数据库文件,都存储在这里。我这里的目录为data/user/0/< package name >/files/     .每个人的手机不同,目录可能不同。
访问方法为:    File filesDir = getFilesDir();





external storage 则是我们外置的SD卡 ,任何应用私有的文件的应该被放置在 getExternalFilesDir返回的目录下,在应用被卸载的时候,系统会清理的就是这个目录。我这里的目录为 /storage/emulated/0/Android/data/com.example.computer.android_usb/files
访问方法为:   File externalFilesDir = getExternalFilesDir(null);




我们注意到上面的getExternalFilesDir(null)参数传入的为null,这样默认访问的是files文件夹,我们可以指定子文件夹

File externalFilesDir = getExternalFilesDir("Caches");




使用这些方法的时候,记得添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

3.附加的一些方法

3.1当我们手机可以插入TF卡的时候,想要获取TF卡的路径改如何呢,下面的代码可以获取到我们的SD卡的位置,和TF卡的位置。亲测有效。

/**
 * 获取外置SD卡路径以及TF卡的路径
 * <p>
 * 返回的数据:paths.get(0)肯定是外置SD卡的位置,因为它是primary external storage.
 *
 * @return 所有可用于存储的不同的卡的位置,用一个List来保存
 */
public static List<String> getExtSDCardPathList() {
    List<String> paths = new ArrayList<String>();
    String extFileStatus = Environment.getExternalStorageState();
    File extFile = Environment.getExternalStorageDirectory();
    //首先判断一下外置SD卡的状态,处于挂载状态才能获取的到
    if (extFileStatus.equals(Environment.MEDIA_MOUNTED)
            && extFile.exists() && extFile.isDirectory()
            && extFile.canWrite()) {
        //外置SD卡的路径
        paths.add(extFile.getAbsolutePath());
    }
    try {
        // obtain executed result of command line code of 'mount', to judge
        // whether tfCard exists by the result
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec("mount");
        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line = null;
        int mountPathIndex = 1;
        while ((line = br.readLine()) != null) {
            // format of sdcard file system: vfat/fuse
            if ((!line.contains("fat") && !line.contains("fuse") && !line
                    .contains("storage"))
                    || line.contains("secure")
                    || line.contains("asec")
                    || line.contains("firmware")
                    || line.contains("shell")
                    || line.contains("obb")
                    || line.contains("legacy") || line.contains("data")) {
                continue;
            }
            String[] parts = line.split(" ");
            int length = parts.length;
            if (mountPathIndex >= length) {
                continue;
            }
            String mountPath = parts[mountPathIndex];
            if (!mountPath.contains("/") || mountPath.contains("data")
                    || mountPath.contains("Data")) {
                continue;
            }
            File mountRoot = new File(mountPath);
            if (!mountRoot.exists() || !mountRoot.isDirectory()
                    || !mountRoot.canWrite()) {
                continue;
            }
            boolean equalsToPrimarySD = mountPath.equals(extFile
                    .getAbsolutePath());
            if (equalsToPrimarySD) {
                continue;
            }
            //扩展存储卡即TF卡或者SD卡路径
            paths.add(mountPath);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return paths;
}


3.2 删除文件夹和指定文件夹下的所有文件 从:http://blog.csdn.net/cat_book_milk/article/details/53586070 来
  1. //删除文件夹  
  2. public static void delFolder(String folderPath) {  
  3.      try {  
  4.         delAllFile(folderPath); //删除完里面所有内容  
  5.         String filePath = folderPath;  
  6.         filePath = filePath.toString();  
  7.         java.io.File myFilePath = new java.io.File(filePath);  
  8.         myFilePath.delete(); //删除空文件夹  
  9.      } catch (Exception e) {  
  10.        e.printStackTrace();   
  11.      }  
  12. }  

删除指定文件夹下的所有文件

[java]   view plain  copy
  1. public static boolean delAllFile(String path) {  
  2.        boolean flag = false;  
  3.        File file = new File(path);  
  4.        if (!file.exists()) {  
  5.          return flag;  
  6.        }  
  7.        if (!file.isDirectory()) {  
  8.          return flag;  
  9.        }  
  10.        String[] tempList = file.list();  
  11.        File temp = null;  
  12.        for (int i = 0; i < tempList.length; i++) {  
  13.           if (path.endsWith(File.separator)) {  
  14.              temp = new File(path + tempList[i]);  
  15.           } else {  
  16.               temp = new File(path + File.separator + tempList[i]);  
  17.           }  
  18.           if (temp.isFile()) {  
  19.              temp.delete();  
  20.           }  
  21.           if (temp.isDirectory()) {  
  22.              delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件  
  23.              delFolder(path + "/" + tempList[i]);//再删除空文件夹  
  24.              flag = true;  
  25.           }  
  26.        }  
  27.        return flag;  
  28.      }  
  29. }  







猜你喜欢

转载自blog.csdn.net/a260724032/article/details/78275740