`
xiaoyaoniu
  • 浏览: 189345 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

android的Environment类 .

 
阅读更多

1、讲述 Environment 类 
Environment 是一个提供访问环境变量的类。

 
Environment 包含常量: 


MEDIA_BAD_REMOVAL 
解释:返回getExternalStorageState() ,表明SDCard 被卸载前己被移除 


MEDIA_CHECKING 
解释:返回getExternalStorageState() ,表明对象正在磁盘检查。 


MEDIA_MOUNTED 
解释:返回getExternalStorageState() ,表明对象是否存在并具有读/写权限 


MEDIA_MOUNTED_READ_ONLY 
解释:返回getExternalStorageState() ,表明对象权限为只读 


MEDIA_NOFS 
解释:返回getExternalStorageState() ,表明对象为空白或正在使用不受支持的文件系统。

 
MEDIA_REMOVED 
解释:返回getExternalStorageState() ,如果不存在 SDCard 返回 


MEDIA_SHARED 
解释:返回getExternalStorageState() ,如果 SDCard 未安装 ,并通过 USB 大容量存储共享 返回 

MEDIA_UNMOUNTABLE 
解释:返回getExternalStorageState() ,返回 SDCard 不可被安装 如果 SDCard 是存在但不可以被安装 


MEDIA_UNMOUNTED 
解释:返回getExternalStorageState() ,返回 SDCard 已卸掉如果 SDCard   是存在但是没有被安装 


Environment 常用方法: 
方法:getDataDirectory() 
解释:返回 File ,获取 Android 数据目录。 


方法:getDownloadCacheDirectory() 
解释:返回 File ,获取 Android 下载/缓存内容目录。

 
方法:getExternalStorageDirectory() 
解释:返回 File ,获取外部存储目录即 SDCard 


方法:getExternalStoragePublicDirectory(String type) 
解释:返回 File ,取一个高端的公用的外部存储器目录来摆放某些类型的文件 


方法:getExternalStorageState() 
解释:返回 File ,获取外部存储设备的当前状态 


方法:getRootDirectory() 
解释:返回 File ,获取 Android 的根目录 


2、讲述 StatFs 类 
StatFs 一个模拟linux的df命令的一个类,获得SD卡和手机内存的使用情况 


StatFs 常用方法: 


getAvailableBlocks() 
解释:返回 Int ,获取当前可用的存储空间 


getBlockCount() 
解释:返回 Int ,获取该区域可用的文件系统数 


getBlockSize() 
解释:返回 Int ,大小,以字节为单位,一个文件系统 


getFreeBlocks() 
解释:返回 Int ,该块区域剩余的空间 


restat(String path) 
解释:执行一个由该对象所引用的文件系统 

 

 

 

 

 

 

   

Constants

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:

 

 

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

    try {
        // Make sure the Pictures directory exists.
        path.mkdirs();

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

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

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

boolean hasExternalStoragePublicPicture() {
    // Create a path where we will place our picture in the user's
    // public pictures directory and check if the file exists.  If
    // external storage is not currently mounted this will think the
    // picture doesn't exist.
    File path = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File file = new File(path, "DemoPicture.jpg");
    return file.exists();
}

 

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().

 

  

 

分享到:
评论

相关推荐

    Android.App.Development.in.Android.Studio.Java.Android.Edition.For.Beginners.pdf

    This book covers Android app design fundamentals in Android Studio using Java programming language. The author assumes you have no experience in app development. The book starts with the installation ...

    android_environment.zip_android_安卓配置环境

    这个"android_environment.zip"文件正是为了帮助初学者快速、顺利地完成这一过程。本文将详细解析这个压缩包内的"android环境配置.doc"文档所涵盖的知识点,并提供一份详尽的Android开发环境配置指南。 1. **Java ...

    android_studio_2020.3.1.zip

    Android Studio是谷歌为Android开发者推出的一款强大的集成开发环境(Integrated Development Environment,IDE),它为开发者提供了全面的工具集,用于构建、调试和发布Android应用程序。2020.3.1版本的Android ...

    android-studio-2021.2.1.15-windows软件包

    Android Studio,作为谷歌官方推出的Android应用程序开发集成环境(Integrated Development Environment, IDE),是全球开发者广泛使用的工具之一。这个名为“android-studio-2021.2.1.15-windows”的软件包,正是专...

    Mastering.Android.Wear.Application.Development

    Set up a development environment to build Android Wear apps using Android Studio Master the Android Wear SDK and APIs Understand the UI patterns and UX principles to build Android Wear apps Work with ...

    安卓开发-Android 主流的图片浏览的全屏缩放效果SmoothImageDemo软件源码

    安卓开发-Android 主流的图片浏览的...import android.os.Environment; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.wi

    android 查询存储卡剩余容量 (Environment 加 StatFs)

    String path = Environment.getExternalStorageDirectory().getPath(); // 创建StatFs对象 StatFs statFs = new StatFs(path); // 计算剩余可用空间(单位:字节) long blockSize = statFs.getBlockSizeLong...

    Android.application.development

    Environment Setup Chapter 3. Architecture Chapter 4. Application Components Chapter 5. Hello World Example Chapter 6. Resources Organizing & Accessing Chapter 7. ACCESSING RESOURCES IN CODE Chapter 8...

    test_surface_test_

    public class DecodeActivity extends Activity implements SurfaceHolder.Callback { // private static final String strVideo = // Environment.getExternalStorageDirectory() + "/test/xx.avi"; // ...

    Beginning.Android.CPP.Game.Development

    附录A“Using the Android Development Environment”(使用Android开发环境)将为读者提供关于如何配置和使用Android开发环境,包括搭建开发环境、安装必要的软件、以及开发工具的使用方法等内容。 整体来看,本书...

    新版Android开发教程.rar

    ----------------------------------- Android 编程基础 1 封面----------------------------------- Android 编程基础 2 开放手机联盟 --Open --Open --Open --Open Handset Handset Handset Handset Alliance ...

    Android_NDK.zip_Android so库_NDK SO_android_android so_调用.so

    - 在Android中,通常将文件存储在SD卡(`Environment.getExternalStorageDirectory()`)或其他可用的存储区域。 5. **Android.mk与CMakeLists.txt**: - Android.mk是旧版的构建系统,用于配置NDK编译过程,定义...

    android_environment5

    android_environment5android_environment5

Global site tag (gtag.js) - Google Analytics