android的Environment类

String

MEDIA_BAD_REMOVAL

在没有挂载前存储媒体已经被移除。

String

MEDIA_CHECKING

正在检查存储媒体。

String

MEDIA_MOUNTED

存储媒体已经挂载,并且挂载点可读/写。

String

MEDIA_MOUNTED_READ_ONLY

存储媒体已经挂载,挂载点只读。

String

MEDIA_NOFS

存储媒体是空白或是不支持的文件系统。

String

MEDIA_REMOVED

存储媒体被移除。

String

MEDIA_SHARED

存储媒体正在通过USB共享。

String

MEDIA_UNMOUNTABLE

存储媒体无法挂载。

String

MEDIA_UNMOUNTED

存储媒体没有挂载。

Fields

public static String

DIRECTORY_ALARMS

系统提醒铃声存放的标准目录。

public static String

DIRECTORY_DCIM

相机拍摄照片和视频的标准目录。

public static String

DIRECTORY_DOWNLOADS

下载的标准目录。

public static String

DIRECTORY_MOVIES

电影存放的标准目录。

public static String

DIRECTORY_MUSIC

音乐存放的标准目录。

public static String

DIRECTORY_NOTIFICATIONS

系统通知铃声存放的标准目录。

public static String

DIRECTORY_PICTURES

图片存放的标准目录。

public static String

DIRECTORY_PODCASTS

系统广播存放的标准目录。

public static String

DIRECTORY_RINGTONES

系统铃声存放的标准目录。

Public Methods

static File

getDataDirectory()

获得android data的目录。

static File

getDownloadCacheDirectory()

获得下载缓存目录。

static File

getExternalStorageDirectory()

或者外部存储媒体目录。

static File

getExternalStoragePublicDirectory( String type)

Get a top-level public external storage directory for placing files of a particular type.

static String

getExternalStorageState()

获得当前外部储存媒体的状态。

static File

getRootDirectory()

获得android的跟目录。

public static File getExternalStoragePublicDirectory (String type)

Since: API Level 8

Get a top-level public external storage directory for placing files of a particular type. This is where the user will typically place and manage their own files, so you should be careful about what you put here to ensure you don't erase their files or get in the way of their own organization.

Here is an example of typical code to manipulate a picture on the public external storage:

<span class="kwd">void</span><span class="pln"> createExternalStoragePublicPicture</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="com">// Create a path where we will place our picture in the user's</span><span class="pln">
    </span><span class="com">// public pictures directory.  Note that you should be careful about</span><span class="pln">
    </span><span class="com">// what you place here, since the user often manages these files.  For</span><span class="pln">
    </span><span class="com">// pictures and other media owned by the application, consider</span><span class="pln">
    </span><span class="com">// Context.getExternalMediaDir().</span><span class="pln">
    </span><span class="typ">File</span><span class="pln"> path </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Environment</span><span class="pun">.</span><span class="pln">getExternalStoragePublicDirectory</span><span class="pun">(</span><span class="pln">
            </span><span class="typ">Environment</span><span class="pun">.</span><span class="pln">DIRECTORY_PICTURES</span><span class="pun">);</span><span class="pln">
    </span><span class="typ">File</span><span class="pln"> file </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">File</span><span class="pun">(</span><span class="pln">path</span><span class="pun">,</span><span class="pln"> </span><span class="str">"DemoPicture.jpg"</span><span class="pun">);</span><span class="pln">

    </span><span class="kwd">try</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        </span><span class="com">// Make sure the Pictures directory exists.</span><span class="pln">
        path</span><span class="pun">.</span><span class="pln">mkdirs</span><span class="pun">();</span><span class="pln">

        </span><span class="com">// Very simple code to copy a picture from the application's</span><span class="pln">
        </span><span class="com">// resource into the external file.  Note that this code does</span><span class="pln">
        </span><span class="com">// no error checking, and assumes the picture is small (does not</span><span class="pln">
        </span><span class="com">// try to copy it in chunks).  Note that if external storage is</span><span class="pln">
        </span><span class="com">// not currently mounted this will silently fail.</span><span class="pln">
        </span><span class="typ">InputStream</span><span class="pln"> </span><span class="kwd">is</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> getResources</span><span class="pun">().</span><span class="pln">openRawResource</span><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">drawable</span><span class="pun">.</span><span class="pln">balloons</span><span class="pun">);</span><span class="pln">
        </span><span class="typ">OutputStream</span><span class="pln"> os </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">FileOutputStream</span><span class="pun">(</span><span class="pln">file</span><span class="pun">);</span><span class="pln">
        </span><span class="kwd">byte</span><span class="pun">[]</span><span class="pln"> data </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="kwd">byte</span><span class="pun">[</span><span class="kwd">is</span><span class="pun">.</span><span class="pln">available</span><span class="pun">()];</span><span class="pln">
        </span><span class="kwd">is</span><span class="pun">.</span><span class="pln">read</span><span class="pun">(</span><span class="pln">data</span><span class="pun">);</span><span class="pln">
        os</span><span class="pun">.</span><span class="pln">write</span><span class="pun">(</span><span class="pln">data</span><span class="pun">);</span><span class="pln">
        </span><span class="kwd">is</span><span class="pun">.</span><span class="pln">close</span><span class="pun">();</span><span class="pln">
        os</span><span class="pun">.</span><span class="pln">close</span><span class="pun">();</span><span class="pln">

        </span><span class="com">// Tell the media scanner about the new file so that it is // 这一步非常重要!!</span><span class="pln">
        </span><span class="com">// immediately available to the user.</span><span class="pln">
        </span><span class="typ">MediaScannerConnection</span><span class="pun">.</span><span class="pln">scanFile</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">,</span><span class="pln">
                </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">String</span><span class="pun">[]</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> file</span><span class="pun">.</span><span class="pln">toString</span><span class="pun">()</span><span class="pln"> </span><span class="pun">},</span><span class="pln"> </span><span class="kwd">null</span><span class="pun">,</span><span class="pln">
                </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">MediaScannerConnection</span><span class="pun">.</span><span class="typ">OnScanCompletedListener</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
            </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> onScanCompleted</span><span class="pun">(</span><span class="typ">String</span><span class="pln"> path</span><span class="pun">,</span><span class="pln"> </span><span class="typ">Uri</span><span class="pln"> uri</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
                </span><span class="typ">Log</span><span class="pun">.</span><span class="pln">i</span><span class="pun">(</span><span class="str">"ExternalStorage"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"Scanned "</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> path </span><span class="pun">+</span><span class="pln"> </span><span class="str">":"</span><span class="pun">);</span><span class="pln">
                </span><span class="typ">Log</span><span class="pun">.</span><span class="pln">i</span><span class="pun">(</span><span class="str">"ExternalStorage"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"-> uri="</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> uri</span><span class="pun">);</span><span class="pln">
            </span><span class="pun">}</span><span class="pln">
        </span><span class="pun">});</span><span class="pln">
    </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">catch</span><span class="pln"> </span><span class="pun">(</span><span class="typ">IOException</span><span class="pln"> e</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        </span><span class="com">// Unable to create file, likely because external storage is</span><span class="pln">
        </span><span class="com">// not currently mounted.</span><span class="pln">
        </span><span class="typ">Log</span><span class="pun">.</span><span class="pln">w</span><span class="pun">(</span><span class="str">"ExternalStorage"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"Error writing "</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> file</span><span class="pun">,</span><span class="pln"> e</span><span class="pun">);</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

</span><span class="kwd">void</span><span class="pln"> deleteExternalStoragePublicPicture</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="com">// Create a path where we will place our picture in the user's</span><span class="pln">
    </span><span class="com">// public pictures directory and delete the file.  If external</span><span class="pln">
    </span><span class="com">// storage is not currently mounted this will fail.</span><span class="pln">
    </span><span class="typ">File</span><span class="pln"> path </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Environment</span><span class="pun">.</span><span class="pln">getExternalStoragePublicDirectory</span><span class="pun">(</span><span class="pln">
            </span><span class="typ">Environment</span><span class="pun">.</span><span class="pln">DIRECTORY_PICTURES</span><span class="pun">);</span><span class="pln">
    </span><span class="typ">File</span><span class="pln"> file </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">File</span><span class="pun">(</span><span class="pln">path</span><span class="pun">,</span><span class="pln"> </span><span class="str">"DemoPicture.jpg"</span><span class="pun">);</span><span class="pln">
    file</span><span class="pun">.</span><span class="kwd">delete</span><span class="pun">();</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

</span><span class="kwd">boolean</span><span class="pln"> hasExternalStoragePublicPicture</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="com">// Create a path where we will place our picture in the user's</span><span class="pln">
    </span><span class="com">// public pictures directory and check if the file exists.  If</span><span class="pln">
    </span><span class="com">// external storage is not currently mounted this will think the</span><span class="pln">
    </span><span class="com">// picture doesn't exist.</span><span class="pln">
    </span><span class="typ">File</span><span class="pln"> path </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Environment</span><span class="pun">.</span><span class="pln">getExternalStoragePublicDirectory</span><span class="pun">(</span><span class="pln">
            </span><span class="typ">Environment</span><span class="pun">.</span><span class="pln">DIRECTORY_PICTURES</span><span class="pun">);</span><span class="pln">
    </span><span class="typ">File</span><span class="pln"> file </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">File</span><span class="pun">(</span><span class="pln">path</span><span class="pun">,</span><span class="pln"> </span><span class="str">"DemoPicture.jpg"</span><span class="pun">);</span><span class="pln">
    </span><span class="kwd">return</span><span class="pln"> file</span><span class="pun">.</span><span class="pln">exists</span><span class="pun">();</span><span class="pln">
</span><span class="pun">}</span>
Parameters
type The type of storage directory to return. Should be one of DIRECTORY_MUSIC,DIRECTORY_PODCASTS,DIRECTORY_RINGTONES,DIRECTORY_ALARMS,DIRECTORY_NOTIFICATIONS,DIRECTORY_PICTURES,DIRECTORY_MOVIES,DIRECTORY_DOWNLOADS, orDIRECTORY_DCIM. May not be null.
Returns
  • Returns the File path for the directory. Note that this directory may not yet exist, so you must make sure it exists before using it such as withFile.mkdirs().

以上内容摘自google android开发文

你可能感兴趣的:(android的Environment类)