Android Environment 获取各种路径的方法

时间:2021-07-26 09:53:14
  1. <pre name="code" class="java">package com.deepoon.beyond.environment;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.os.Bundle;
  5. import android.os.Environment;
  6. import android.widget.TextView;
  7. import com.deepoon.beyond.R;
  8. public class EnvironmentActivity extends Activity {
  9. private Context mContext;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. mContext = this;
  14. setContentView(R.layout.activity_environment);
  15. TextView textViewDataDirectory = (TextView) findViewById(R.id.textview_data_directory);
  16. textViewDataDirectory.setText(Environment.getDataDirectory().getPath());
  17. TextView textViewRootDirectory = (TextView) findViewById(R.id.textview_root_directory);
  18. textViewRootDirectory.setText(Environment.getRootDirectory().getPath());
  19. TextView textViewExternalStorageState = (TextView) findViewById(R.id.textview_external_storage_state);
  20. textViewExternalStorageState.setText(Environment.getExternalStorageState().toString());
  21. TextView textViewDownloadCacheDirectory = (TextView) findViewById(R.id.textview_download_cache_directory);
  22. textViewDownloadCacheDirectory.setText(Environment.getDownloadCacheDirectory().getPath());
  23. TextView textViewExternalStorageDirectory = (TextView) findViewById(R.id.textview_external_storage_directory);
  24. textViewExternalStorageDirectory.setText(Environment.getExternalStorageDirectory().getPath());
  25. TextView textViewExternalStorageEmulated = (TextView) findViewById(R.id.textview_external_storage_emulated);
  26. textViewExternalStorageEmulated.setText("设备的外存是否是用内存模拟的: " + Environment.isExternalStorageEmulated());
  27. TextView textViewExternalStorageRemovable = (TextView) findViewById(R.id.textview_external_storage_removable);
  28. textViewExternalStorageRemovable.setText("设备的外存是否是可以拆卸的: " + Environment.isExternalStorageRemovable());
  29. TextView textViewDirectoryAlarms = (TextView) findViewById(R.id.textview_directory_alarms);
  30. textViewDirectoryAlarms.setText(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_ALARMS).getPath());
  31. TextView textViewDirectoryDCIM = (TextView) findViewById(R.id.textview_directory_dcim);
  32. textViewDirectoryDCIM.setText(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath());
  33. TextView textViewDirectoryDocuments = (TextView) findViewById(R.id.textview_directory_documents);
  34. textViewDirectoryDocuments.setText(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getPath());
  35. TextView textViewDirectoryDownloads = (TextView) findViewById(R.id.textview_directory_downloads);
  36. textViewDirectoryDownloads.setText(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath());
  37. TextView textViewDirectoryMovies = (TextView) findViewById(R.id.textview_directory_movies);
  38. textViewDirectoryMovies.setText(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getPath());
  39. TextView textViewDirectoryMusic = (TextView) findViewById(R.id.textview_directory_music);
  40. textViewDirectoryMusic.setText(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getPath());
  41. TextView textViewDirectoryNotifications = (TextView) findViewById(R.id.textview_directory_notifications);
  42. textViewDirectoryNotifications.setText(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_NOTIFICATIONS).getPath());
  43. TextView textViewDirectoryPictures = (TextView) findViewById(R.id.textview_directory_pictures);
  44. textViewDirectoryPictures.setText(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath());
  45. TextView textViewDirectoryPodcasts = (TextView) findViewById(R.id.textview_directory_podcasts);
  46. textViewDirectoryPodcasts.setText(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PODCASTS).getPath());
  47. TextView textViewDirectoryRingtones = (TextView) findViewById(R.id.textview_directory_ringtones);
  48. textViewDirectoryRingtones.setText(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES).getPath());
  49. TextView textViewFilesDir = (TextView) findViewById(R.id.textview_files_dir);
  50. textViewFilesDir.setText(mContext.getFilesDir().getPath());
  51. TextView textViewCacheDir = (TextView) findViewById(R.id.textview_cache_dir);
  52. textViewCacheDir.setText(mContext.getCacheDir().getPath());
  53. TextView textViewExternalFilesDir = (TextView) findViewById(R.id.textview_external_files_dir);
  54. textViewExternalFilesDir.setText(mContext.getExternalFilesDir(Environment.DIRECTORY_MOVIES).getPath());
  55. TextView textViewExternalCacheDir = (TextView) findViewById(R.id.textview_external_cache_dir);
  56. textViewExternalCacheDir.setText(mContext.getExternalCacheDir().getPath());
  57. }
  58. }
  1. </pre><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. >
  7. <ScrollView
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent">
  10. <LinearLayout
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. android:gravity="center"
  14. android:orientation="vertical">
  15. <TextView
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="------------------Environment API-----------------------"
  19. android:textSize="14sp"/>
  20. <TextView
  21. android:id="@+id/textview_root_directory"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:textSize="14sp"/>
  25. <TextView
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:text="Environment.getRootDirectory()"
  29. android:textSize="14sp"/>
  30. <TextView
  31. android:id="@+id/textview_data_directory"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:layout_marginTop="10dp"
  35. android:textSize="14sp"/>
  36. <TextView
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:text="Environment.getDataDirectory()"
  40. android:textSize="14sp"/>
  41. <TextView
  42. android:id="@+id/textview_external_storage_state"
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:layout_marginTop="10dp"
  46. android:textSize="14sp"/>
  47. <TextView
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. android:text="Environment.getExternalStorageState()"
  51. android:textSize="14sp"/>
  52. <TextView
  53. android:id="@+id/textview_download_cache_directory"
  54. android:layout_width="wrap_content"
  55. android:layout_height="wrap_content"
  56. android:layout_marginTop="10dp"
  57. android:textSize="14sp"/>
  58. <TextView
  59. android:layout_width="wrap_content"
  60. android:layout_height="wrap_content"
  61. android:text="Environment.getDownloadCacheDirectory()"
  62. android:textSize="14sp"/>
  63. <TextView
  64. android:id="@+id/textview_external_storage_directory"
  65. android:layout_width="wrap_content"
  66. android:layout_height="wrap_content"
  67. android:layout_marginTop="10dp"
  68. android:textSize="14sp"/>
  69. <TextView
  70. android:layout_width="wrap_content"
  71. android:layout_height="wrap_content"
  72. android:text="Environment.getExternalStorageDirectory()"
  73. android:textSize="14sp"/>
  74. <TextView
  75. android:id="@+id/textview_external_storage_emulated"
  76. android:layout_width="wrap_content"
  77. android:layout_height="wrap_content"
  78. android:layout_marginTop="10dp"
  79. android:textSize="14sp"/>
  80. <TextView
  81. android:layout_width="wrap_content"
  82. android:layout_height="wrap_content"
  83. android:text="Environment.isExternalStorageEmulated()"
  84. android:textSize="14sp"/>
  85. <TextView
  86. android:id="@+id/textview_external_storage_removable"
  87. android:layout_width="wrap_content"
  88. android:layout_height="wrap_content"
  89. android:layout_marginTop="10dp"
  90. android:textSize="14sp"/>
  91. <TextView
  92. android:layout_width="wrap_content"
  93. android:layout_height="wrap_content"
  94. android:text="Environment.isExternalStorageRemovable()"
  95. android:textSize="14sp"/>
  96. <TextView
  97. android:layout_width="wrap_content"
  98. android:layout_height="wrap_content"
  99. android:layout_marginTop="10dp"
  100. android:text="注意:传入的类型参数不能是null,返回的目录路径有可能不存在,所以必须在使用之前确认一下,比如使用File.mkdirs创建该路径"
  101. android:textSize="14sp"/>
  102. <TextView
  103. android:id="@+id/textview_directory_alarms"
  104. android:layout_width="wrap_content"
  105. android:layout_height="wrap_content"
  106. android:textSize="14sp"/>
  107. <TextView
  108. android:layout_width="wrap_content"
  109. android:layout_height="wrap_content"
  110. android:text="系统提醒铃声: Environment.DIRECTORY_ALARMS"
  111. android:textSize="14sp"/>
  112. <TextView
  113. android:id="@+id/textview_directory_dcim"
  114. android:layout_width="wrap_content"
  115. android:layout_height="wrap_content"
  116. android:layout_marginTop="10dp"
  117. android:textSize="14sp"/>
  118. <TextView
  119. android:layout_width="wrap_content"
  120. android:layout_height="wrap_content"
  121. android:text="相机拍摄照片和视频: Environment.DIRECTORY_DCIM"
  122. android:textSize="14sp"/>
  123. <TextView
  124. android:id="@+id/textview_directory_documents"
  125. android:layout_width="wrap_content"
  126. android:layout_height="wrap_content"
  127. android:layout_marginTop="10dp"
  128. android:textSize="14sp"/>
  129. <TextView
  130. android:layout_width="wrap_content"
  131. android:layout_height="wrap_content"
  132. android:text="文档: Environment.DIRECTORY_DOCUMENTS"
  133. android:textSize="14sp"/>
  134. <TextView
  135. android:id="@+id/textview_directory_downloads"
  136. android:layout_width="wrap_content"
  137. android:layout_height="wrap_content"
  138. android:layout_marginTop="10dp"
  139. android:textSize="14sp"/>
  140. <TextView
  141. android:layout_width="wrap_content"
  142. android:layout_height="wrap_content"
  143. android:text="下载: Environment.DIRECTORY_DOWNLOADS"
  144. android:textSize="14sp"/>
  145. <TextView
  146. android:id="@+id/textview_directory_movies"
  147. android:layout_width="wrap_content"
  148. android:layout_height="wrap_content"
  149. android:layout_marginTop="10dp"
  150. android:textSize="14sp"/>
  151. <TextView
  152. android:layout_width="wrap_content"
  153. android:layout_height="wrap_content"
  154. android:text="电影: Environment.DIRECTORY_MOVIES"
  155. android:textSize="14sp"/>
  156. <TextView
  157. android:id="@+id/textview_directory_music"
  158. android:layout_width="wrap_content"
  159. android:layout_height="wrap_content"
  160. android:layout_marginTop="10dp"
  161. android:textSize="14sp"/>
  162. <TextView
  163. android:layout_width="wrap_content"
  164. android:layout_height="wrap_content"
  165. android:text="音乐: Environment.DIRECTORY_MUSIC"
  166. android:textSize="14sp"/>
  167. <TextView
  168. android:id="@+id/textview_directory_notifications"
  169. android:layout_width="wrap_content"
  170. android:layout_height="wrap_content"
  171. android:layout_marginTop="10dp"
  172. android:textSize="14sp"/>
  173. <TextView
  174. android:layout_width="wrap_content"
  175. android:layout_height="wrap_content"
  176. android:text="系统通知铃声: Environment.DIRECTORY_NOTIFICATIONS"
  177. android:textSize="14sp"/>
  178. <TextView
  179. android:id="@+id/textview_directory_pictures"
  180. android:layout_width="wrap_content"
  181. android:layout_height="wrap_content"
  182. android:layout_marginTop="10dp"
  183. android:textSize="14sp"/>
  184. <TextView
  185. android:layout_width="wrap_content"
  186. android:layout_height="wrap_content"
  187. android:text="图片: Environment.DIRECTORY_PICTURES"
  188. android:textSize="14sp"/>
  189. <TextView
  190. android:id="@+id/textview_directory_podcasts"
  191. android:layout_width="wrap_content"
  192. android:layout_height="wrap_content"
  193. android:layout_marginTop="10dp"
  194. android:textSize="14sp"/>
  195. <TextView
  196. android:layout_width="wrap_content"
  197. android:layout_height="wrap_content"
  198. android:text="系统广播: Environment.DIRECTORY_PODCASTS"
  199. android:textSize="14sp"/>
  200. <TextView
  201. android:id="@+id/textview_directory_ringtones"
  202. android:layout_width="wrap_content"
  203. android:layout_height="wrap_content"
  204. android:layout_marginTop="10dp"
  205. android:textSize="14sp"/>
  206. <TextView
  207. android:layout_width="wrap_content"
  208. android:layout_height="wrap_content"
  209. android:text="系统铃声: Environment.DIRECTORY_RINGTONES"
  210. android:textSize="14sp"/>
  211. <TextView
  212. android:layout_width="wrap_content"
  213. android:layout_height="wrap_content"
  214. android:text="--------------------Context API------------------------"
  215. android:layout_marginTop="10dp"
  216. android:textSize="14sp"/>
  217. <TextView
  218. android:id="@+id/textview_files_dir"
  219. android:layout_width="wrap_content"
  220. android:layout_height="wrap_content"
  221. android:layout_marginTop="10dp"
  222. android:textSize="14sp"/>
  223. <TextView
  224. android:layout_width="wrap_content"
  225. android:layout_height="wrap_content"
  226. android:text="应用的在系统内部Files: Context.getFilesDir()"
  227. android:textSize="14sp"/>
  228. <TextView
  229. android:id="@+id/textview_cache_dir"
  230. android:layout_width="wrap_content"
  231. android:layout_height="wrap_content"
  232. android:layout_marginTop="10dp"
  233. android:textSize="14sp"/>
  234. <TextView
  235. android:layout_width="wrap_content"
  236. android:layout_height="wrap_content"
  237. android:text="应用的在系统内部Cache: Context.getCacheDir()"
  238. android:textSize="14sp"/>
  239. <TextView
  240. android:id="@+id/textview_external_files_dir"
  241. android:layout_width="wrap_content"
  242. android:layout_height="wrap_content"
  243. android:layout_marginTop="10dp"
  244. android:textSize="14sp"/>
  245. <TextView
  246. android:layout_width="wrap_content"
  247. android:layout_height="wrap_content"
  248. android:text="应用的在SDCard Files: Context.getExternalFilesDir()"
  249. android:textSize="14sp"/>
  250. <TextView
  251. android:id="@+id/textview_external_cache_dir"
  252. android:layout_width="wrap_content"
  253. android:layout_height="wrap_content"
  254. android:layout_marginTop="10dp"
  255. android:textSize="14sp"/>
  256. <TextView
  257. android:layout_width="wrap_content"
  258. android:layout_height="wrap_content"
  259. android:text="应用的在SDCard Files: Context.getExternalCacheDir()"
  260. android:textSize="14sp"/>
  261. </LinearLayout>
  262. </ScrollView>
  263. </LinearLayout>

Android Environment 获取各种路径的方法

Android Environment 获取各种路径的方法