Android -- Environment类

2024-09-01 06:08
文章标签 android environment

本文主要是介绍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();
}

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

这篇关于Android -- Environment类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1126155

相关文章

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

Android App安装列表获取方法(实践方案)

《AndroidApp安装列表获取方法(实践方案)》文章介绍了Android11及以上版本获取应用列表的方案调整,包括权限配置、白名单配置和action配置三种方式,并提供了相应的Java和Kotl... 目录前言实现方案         方案概述一、 androidManifest 三种配置方式

Android WebView无法加载H5页面的常见问题和解决方法

《AndroidWebView无法加载H5页面的常见问题和解决方法》AndroidWebView是一种视图组件,使得Android应用能够显示网页内容,它基于Chromium,具备现代浏览器的许多功... 目录1. WebView 简介2. 常见问题3. 网络权限设置4. 启用 JavaScript5. D

Android如何获取当前CPU频率和占用率

《Android如何获取当前CPU频率和占用率》最近在优化App的性能,需要获取当前CPU视频频率和占用率,所以本文小编就来和大家总结一下如何在Android中获取当前CPU频率和占用率吧... 最近在优化 App 的性能,需要获取当前 CPU视频频率和占用率,通过查询资料,大致思路如下:目前没有标准的

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

Android里面的Service种类以及启动方式

《Android里面的Service种类以及启动方式》Android中的Service分为前台服务和后台服务,前台服务需要亮身份牌并显示通知,后台服务则有启动方式选择,包括startService和b... 目录一句话总结:一、Service 的两种类型:1. 前台服务(必须亮身份牌)2. 后台服务(偷偷干

Android kotlin语言实现删除文件的解决方案

《Androidkotlin语言实现删除文件的解决方案》:本文主要介绍Androidkotlin语言实现删除文件的解决方案,在项目开发过程中,尤其是需要跨平台协作的项目,那么删除用户指定的文件的... 目录一、前言二、适用环境三、模板内容1.权限申请2.Activity中的模板一、前言在项目开发过程中,尤

Android数据库Room的实际使用过程总结

《Android数据库Room的实际使用过程总结》这篇文章主要给大家介绍了关于Android数据库Room的实际使用过程,详细介绍了如何创建实体类、数据访问对象(DAO)和数据库抽象类,需要的朋友可以... 目录前言一、Room的基本使用1.项目配置2.创建实体类(Entity)3.创建数据访问对象(DAO