Android 环境变量访问类

本文介绍了Android系统的存储状态常量及标准目录字段,包括不同存储状态的定义、常见文件类型的默认存储路径等,并提供了获取这些目录及状态的方法示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

摘自:http://developer.android.com/reference/android/os/Environment.html

 

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

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

相关环境变量的值在init.${ro.hardware}.rc 中设置。

 


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

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


示例:http://www.cnblogs.com/mengdd/p/3742623.html


### 设置 Android 开发环境中 JDK 的环境变量 对于 Windows 用户,在设置 JDK 环境变量 `JAVA_HOME` 时,需遵循特定流程以确保集成开发环境 (IDE),如 Android Studio 能够识别所安装的 JDK 版本。当配置 `JAVA_HOME` 变量时,应指向 JDK 安装目录而非 bin 文件夹[^1]。 具体操作如下: #### 配置 Windows 上的 JAVA_HOME 为了使系统范围内的应用程序都能访问到指定版本的 JDK,在系统的环境变量中添加一个新的名为 `JAVA_HOME` 的变量,并将其值设为 JDK 的根目录位置,例如 `C:\Program Files\Java\jdk-17`。这一步骤可通过“我的电脑”-> 属性 -> 高级系统设置 -> 环境变量来完成。接着,在 Path 或 CLASSPATH 中追加 `%JAVA_HOME%\bin`; 这样做可以简化命令行工具调用 Java 编译器和其他实用程序的方式[^2]。 ```batch setx JAVA_HOME "C:\Program Files\Java\jdk-17" set PATH=%JAVA_HOME%\bin;%PATH% ``` 以上批处理脚本用于永久修改当前用户的环境变量而不影响其他用户账户下的设置。 #### Mac OS X/Linux 下的操作指南 在 Unix 操作系统里,则是在终端执行相应指令或者编辑 shell profile 文件(比如 `.bashrc`, `.zshrc`),向其中加入下面两行语句之一,取决于个人喜好以及使用的 Shell 种: ```shell export JAVA_HOME=$(/usr/libexec/java_home) # 对于已知确切路径的情况可以直接写死路径 # export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home export PATH=$JAVA_HOME/bin:$PATH ``` 上述方法适用于大多数情况;然而如果遇到特殊需求或是不同发行版之间的差异,则可能还需要额外调整。 一旦完成了这些更改之后,请记得重启任何正在运行的应用程序以便让新的环境变量生效,特别是 IDEs 和构建服务器等长期驻留内存的服务进程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值