当前位置:   article > 正文

android 文件存储调试细节(小米系统)_getexternalstoragedirectory().getabsolutepath() 小米

getexternalstoragedirectory().getabsolutepath() 小米

android文件永久存储一共有两个位置,内部存储和外部存储,这里的存储是指ROM(断电仍保持记忆),不是运行内存(RAM断电记忆丢失)。

这是由现在android手机的存储结构决定的,内部存储就是焊在手机里的闪存空间的一部分,该部分是app的私有空间,没有root的话,其他软件无法读写该部分空间。焊在手机里的闪存一般分为4g,16g,32g,64g,128g,android的手机一般是4g,16g,32g,64g为多,苹果的手机一般是16g,64g,128g三种选择。

外部存储是除了上面提到的app私有的空间外,以及系统占用的一部分焊在手机里的闪存空间外部分存储空间,而且android手机由于手机用户自行选择插入sdcard这种情况造成了另一部分外部存储空间,如果像苹果那样不能自己插入sdcard,那么就不存在这样的外插sdcard外部存储这种情况.

android实际开发过程中会用到三个地方:

1、internal storage:内部储存,即app私有空间,绝对路径为 “/data/data/app-package-name/”,这部分物理上可以理解为焊在手机里的闪存空间部分存储空间。

     在该目录下会自动创建目录:files,cache,databases,lib,shared_prefs;这些目录因app中产生数据的存储需求而创建,app中没有产生存储需求就可能不产生这些文件夹中的部分。

2、external storage:外部存储空间:焊在手机里的闪存空间的部分,一般绝对路径为:/mnt/shell/emulated/0/(不同系统会有微小的区别,但是对于调用系统提供的函数获得路径而言,这样的区别对编程没有影响),一般该路径可以通过“file:///sdcard/xxx”获得,文件xxx就储存在“/mnt/shell/emulated/0/”目录下。

3、sdcard:外部存储空间,用户自己插入的sdcard,该空间不一定总是存在,所以使用之前需要程序判断是否插入了外部sdcard,外插sdcard根目录的绝对地址为:“/storage/sdcard1/”


为了简化操作,我们只将数据保存在内部存储空间 和 外部存储空间( 焊在手机里),我们可以只将“/data”目录下的存储空间认为是内部存储空间,/mnt/shell/emulated/0 目录下的空间认为是外部存储空间;而且外部存储空间的也分公用的,和私有的

主要参考:http://www.androidchina.net/4106.html

internal storage/data由于内部存储空间有限,在开发中我们一般都是操作外部存储空间,Google官方建议我们App的数据应该存储在外部存储的私有
目录中该App的包名下,这样当用户卸载掉App之后,相关的数据会一并删除,如果你直接在/storage/sdcard目录下创建了一个应
用的文件夹,那么当你删除应用的时候,这个文件夹就不会被删除。
external storage/mnt/shell/emulated/0私有:/mnt/shell/emulated/0/Android/data/app_package_name/files
          /mnt/shell/emulated/0/Android/data/app_package_name/cache

公用:九大公有目录,比如DCIM、DOWNLOAD等这种系统为我们创建的文件夹
sdcard/storage/sdcard1/ 

    

我们的app在使用空间的路径时,应该使用绝对路径,而不是相对路径,关于绝对路径和相对路径,参考:http://blog.csdn.net/androidwifi/article/details/17725989

下面这些函数的方法获得的都是

1、getFilesDir()  = /data/data/com.gsh.gshow/files

2、getExternalFilesDir().getAbsolutePath()  = /storage/emulated/0/Android/data/com.gsh.gshow/files

3、Environment.getDownloadCacheDirectory().getAbsolutePath()  =  /cache

4、Environment.getDataDirectory().getAbsolutePath() = /data

5、Environment.getExternalStorageDirectory().getAbsolutePath() = /storage/emulated/0  (->/mnt/shell/emulated/0)

6、Environment.getExternalStoragePublicDirectory("pub_test")  = /storage/emulated/0


下面这个类操作的就是External storage:

  1. public class SDCardHelper {
  2. // 判断SD卡是否被挂载
  3. public static boolean isSDCardMounted() {
  4. // return Environment.getExternalStorageState().equals("mounted");
  5. return Environment.getExternalStorageState().equals(
  6. Environment.MEDIA_MOUNTED);
  7. }
  8. // 获取SD卡的根目录
  9. public static String getSDCardBaseDir() {
  10. if (isSDCardMounted()) {
  11. return Environment.getExternalStorageDirectory().getAbsolutePath();
  12. }
  13. return null;
  14. }
  15. // 获取SD卡的完整空间大小,返回MB
  16. public static long getSDCardSize() {
  17. if (isSDCardMounted()) {
  18. StatFs fs = new StatFs(getSDCardBaseDir());
  19. long count = fs.getBlockCountLong();
  20. long size = fs.getBlockSizeLong();
  21. return count * size / 1024 / 1024;
  22. }
  23. return 0;
  24. }
  25. // 获取SD卡的剩余空间大小
  26. public static long getSDCardFreeSize() {
  27. if (isSDCardMounted()) {
  28. StatFs fs = new StatFs(getSDCardBaseDir());
  29. long count = fs.getFreeBlocksLong();
  30. long size = fs.getBlockSizeLong();
  31. return count * size / 1024 / 1024;
  32. }
  33. return 0;
  34. }
  35. // 获取SD卡的可用空间大小
  36. public static long getSDCardAvailableSize() {
  37. if (isSDCardMounted()) {
  38. StatFs fs = new StatFs(getSDCardBaseDir());
  39. long count = fs.getAvailableBlocksLong();
  40. long size = fs.getBlockSizeLong();
  41. return count * size / 1024 / 1024;
  42. }
  43. return 0;
  44. }
  45. // 往SD卡的公有目录下保存文件
  46. public static boolean saveFileToSDCardPublicDir(byte[] data, String type, String fileName) {
  47. BufferedOutputStream bos = null;
  48. if (isSDCardMounted()) {
  49. File file = Environment.getExternalStoragePublicDirectory(type);
  50. try {
  51. bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
  52. bos.write(data);
  53. bos.flush();
  54. return true;
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. } finally {
  58. try {
  59. bos.close();
  60. } catch (IOException e) {
  61. // TODO Auto-generated catch block
  62. e.printStackTrace();
  63. }
  64. }
  65. }
  66. return false;
  67. }
  68. // 往SD卡的自定义目录下保存文件
  69. public static boolean saveFileToSDCardCustomDir(byte[] data, String dir, String fileName) {
  70. BufferedOutputStream bos = null;
  71. if (isSDCardMounted()) {
  72. File file = new File(getSDCardBaseDir() + File.separator + dir);
  73. if (!file.exists()) {
  74. file.mkdirs();// 递归创建自定义目录
  75. }
  76. try {
  77. bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
  78. bos.write(data);
  79. bos.flush();
  80. return true;
  81. } catch (Exception e) {
  82. e.printStackTrace();
  83. } finally {
  84. try {
  85. bos.close();
  86. } catch (IOException e) {
  87. // TODO Auto-generated catch block
  88. e.printStackTrace();
  89. }
  90. }
  91. }
  92. return false;
  93. }
  94. // 往SD卡的私有Files目录下保存文件
  95. public static boolean saveFileToSDCardPrivateFilesDir(byte[] data, String type, String fileName, Context context) {
  96. BufferedOutputStream bos = null;
  97. if (isSDCardMounted()) {
  98. File file = context.getExternalFilesDir(type);
  99. try {
  100. bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
  101. bos.write(data);
  102. bos.flush();
  103. return true;
  104. } catch (Exception e) {
  105. e.printStackTrace();
  106. } finally {
  107. try {
  108. bos.close();
  109. } catch (IOException e) {
  110. // TODO Auto-generated catch block
  111. e.printStackTrace();
  112. }
  113. }
  114. }
  115. return false;
  116. }
  117. // 往SD卡的私有Cache目录下保存文件
  118. public static boolean saveFileToSDCardPrivateCacheDir(byte[] data, String fileName, Context context) {
  119. BufferedOutputStream bos = null;
  120. if (isSDCardMounted()) {
  121. File file = context.getExternalCacheDir();
  122. try {
  123. bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
  124. bos.write(data);
  125. bos.flush();
  126. return true;
  127. } catch (Exception e) {
  128. e.printStackTrace();
  129. } finally {
  130. try {
  131. bos.close();
  132. } catch (IOException e) {
  133. // TODO Auto-generated catch block
  134. e.printStackTrace();
  135. }
  136. }
  137. }
  138. return false;
  139. }
  140. // 保存bitmap图片到SDCard的私有Cache目录
  141. public static boolean saveBitmapToSDCardPrivateCacheDir(Bitmap bitmap, String fileName, Context context) {
  142. if (isSDCardMounted()) {
  143. BufferedOutputStream bos = null;
  144. // 获取私有的Cache缓存目录
  145. File file = context.getExternalCacheDir();
  146. try {
  147. bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
  148. if (fileName != null && (fileName.contains(".png") || fileName.contains(".PNG"))) {
  149. bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
  150. } else {
  151. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
  152. }
  153. bos.flush();
  154. } catch (Exception e) {
  155. e.printStackTrace();
  156. } finally {
  157. if (bos != null) {
  158. try {
  159. bos.close();
  160. } catch (IOException e) {
  161. e.printStackTrace();
  162. }
  163. }
  164. }
  165. return true;
  166. } else {
  167. return false;
  168. }
  169. }
  170. // 从SD卡获取文件
  171. public static byte[] loadFileFromSDCard(String fileDir) {
  172. BufferedInputStream bis = null;
  173. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  174. try {
  175. bis = new BufferedInputStream(new FileInputStream(new File(fileDir)));
  176. byte[] buffer = new byte[8 * 1024];
  177. int c = 0;
  178. while ((c = bis.read(buffer)) != -1) {
  179. baos.write(buffer, 0, c);
  180. baos.flush();
  181. }
  182. return baos.toByteArray();
  183. } catch (Exception e) {
  184. e.printStackTrace();
  185. } finally {
  186. try {
  187. baos.close();
  188. bis.close();
  189. } catch (IOException e) {
  190. e.printStackTrace();
  191. }
  192. }
  193. return null;
  194. }
  195. // 从SDCard中寻找指定目录下的文件,返回Bitmap
  196. public Bitmap loadBitmapFromSDCard(String filePath) {
  197. byte[] data = loadFileFromSDCard(filePath);
  198. if (data != null) {
  199. Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
  200. if (bm != null) {
  201. return bm;
  202. }
  203. }
  204. return null;
  205. }
  206. // 获取SD卡公有目录的路径
  207. public static String getSDCardPublicDir(String type) {
  208. return Environment.getExternalStoragePublicDirectory(type).toString();
  209. }
  210. // 获取SD卡私有Cache目录的路径
  211. public static String getSDCardPrivateCacheDir(Context context) {
  212. return context.getExternalCacheDir().getAbsolutePath();
  213. }
  214. // 获取SD卡私有Files目录的路径
  215. public static String getSDCardPrivateFilesDir(Context context, String type) {
  216. return context.getExternalFilesDir(type).getAbsolutePath();
  217. }
  218. public static boolean isFileExist(String filePath) {
  219. File file = new File(filePath);
  220. return file.isFile();
  221. }
  222. // 从sdcard中删除文件
  223. public static boolean removeFileFromSDCard(String filePath) {
  224. File file = new File(filePath);
  225. if (file.exists()) {
  226. try {
  227. file.delete();
  228. return true;
  229. } catch (Exception e) {
  230. return false;
  231. }
  232. } else {
  233. return false;
  234. }
  235. }
  236. }



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/819897
推荐阅读
相关标签
  

闽ICP备14008679号