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:

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android,有多种方式可以存储数据,以下是其一些常见的数据存储方式: 1. Shared Preferences(共享首选项): - Shared Preferences是Android提供的一种轻量级的存储方式,用于存储少量的键值对数据。它适用于存储一些简单的配置信息或用户偏好设置。Shared Preferences使用键值对的方式存储数据,并且只能存储基本数据型。 2. 内部存储: - 内部存储是应用程序私有的文件存储空间,可以用来存储应用程序的私有数据。可以使用File来进行读写操作。内部存储适用于存储较小的数据文件。 3. 外部存储: - 外部存储是指SD卡或其他外部存储设备上的存储空间。Android提供了一些API(如Environment.getExternalStorageDirectory())来访问外部存储。外部存储适用于存储大量的文件或多媒体资源。 4. SQLite数据库: - SQLite是Android内置的关系型数据库,可以用于存储结构化的数据。开发者可以使用SQLiteOpenHelper来创建和管理数据库,以及执行常见的CRUD(创建、读取、更新、删除)操作。 5. ContentProvider(内容提供器): - ContentProvider是用于在应用程序之间共享数据的组件。它提供了标准的接口和方法,允许应用程序对数据进行查询、插入、更新和删除操作。ContentProvider常用于共享数据库或文件等数据。 6. 网络存储: - Android应用程序可以通过网络与服务器进行数据交互,将数据存储在远程服务器上。常用的网络存储方式包括使用HTTP协议与服务器进行通信、使用RESTful API进行数据交互等。 综上所述,Android有多种数据存储方式可供选择,开发者可以根据具体的需求和场景选择合适的方式来存储和管理数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值