• Android Environment 获取各种路径的方法


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

  • 相关阅读:
    倒车入库:场地模型线序
    Mini440之uboot移植之实践NAND启动(四)
    数据库mysql转为postgresql变动
    嵌入式Linux之vs code开发环境搭建
    Chrome Version 19.0.1055.1 dev Flash Missing plugin的修复
    再次解决,android 2.3运行凯立德问题
    笔记本双屏系统的组建
    gitfatal: unable to access : The requested URL returned error: 403
    C++ 学习拾遗 —— 点滴记录C++学习过程中遇到的问题以及整理
    linux 常用命令
  • 原文地址:https://www.cnblogs.com/exmyth/p/8417628.html
Copyright © 2020-2023  润新知