Android中关于内部存储的一些重要函数

一、简介

Android中,你也可以通过绝对路径以JAVA传统方式访问内部存储空间。但是以这种方式创建的文件是对私有,创建它的应用程序对该文件是可读可写,但是别的应用程序并不能直接访问它。不是所有的内部存储空间应用程序都可以访问,默认情况下只能访问“/data/data/你的应用程序的包名”这个路径下的文件。

Android中,你还可以使用Context对象的来进行数据持久化存储的这种方式,你的数据文件将存储在内部存储空间的/data/data/你的应用程序的包名/files/目录下,无法指定更深一级的目录,而且默认是Context.MODE_PRIVATE模式,即别的应用程序不能访问它。你可以使用的int mode参数来让别的应用程序也能访问你的文件。

注意:保存在/data/data/你的应用程序的包名目录中文件,会在卸载你的应用程序时被删除掉。

二、Context中关于内部存储的重要函数

public abstract File getCacheDir ()

API Level 1

Returns the absolute path to the application specific cache directory on
the filesystem. These files will be ones that get deleted first when
the device runs low on storage. There is no guarantee when these files
will be deleted. Note: you should not rely on
the system deleting these files for you; you should always have a
reasonable maximum, such as 1 MB, for the amount of space you consume
with cache files, and prune those files when exceeding that space.

存储空间剩余多少的情况,没有严格的标准保障。
注意:你不应该依赖系统来清理这些缓存文件,你应该对这些缓存文件占用的最大存储空间设定个最大值,比如是1M,当实际占用空间超过这个值时,你应该对这些缓存文件做相应的清理工作(prune)。
Returns
  • Returns the path of the directory holding application cache files.
See Also
  • openFileOutput(String,
    int)
  • getFileStreamPath(String)
  • getDir(String,
    int)
  • import android.app.Activity;
  • import android.content.Context;
  • import android.os.Bundle;
  • import android.util.Log;
  • public class MainActivity extends Activity {
  • final static String TAG="robin";
  • /** Called when the activity is first created. */
  • @Override
  • public void onCreate(Bundle savedInstanceState) {
  • super.onCreate(savedInstanceState);
  • setContentView(R.layout.main);
  • Context context=this;
  • String path=context.getCacheDir().getAbsolutePath();
  • Log.i(TAG,"path:"+path);
  • }
  • }
  • path:/data/data/com.lenovo/cache

public abstract File getDir (String name,
int mode)

API Level 1

Retrieve, creating if needed, a new directory in which the application
can place its own custom data files. You can use the returned File
object to create and access files in this directory. Note that files
created through a File object will only be accessible
by your own application; you can only set the mode of the entire
directory, not of individual files.

:参数是指文件夹的访问权限而并不包括其子文件夹和文件的访问权限
Parameters

name

Name of the directory to retrieve. This is a directory that is created as part of your application data.
mode Operating mode. Use 0 or MODE_PRIVATE for
the default operation, MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE to
control permissions.
Returns
  • Returns a File object for the requested directory. The directory will have been created if it does not already exist.
See Also

String FILENAME = "hello_file";

String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);

fos.write(string.getBytes());

fos.close();

示例6

String FILENAME = "hello_file";

String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);

fos.write(string.getBytes());

fos.close();

示例7

String FILENAME = "hello_file";

String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_APPEND|Context.MODE_WORLD_READABLE;

fos.write(string.getBytes());

fos.close();

public abstract boolean deleteFile (String name)

API Level 1

Delete the given private file associated with this Context‘s application package.

Parameters

name

The name of the file to delete; can not contain path separators.
Returns
  • True if the file was successfully deleted; else false.
See Also

public abstract String[] fileList ()

API Level 1

Returns an array of strings naming the private files associated with this Context‘s application package.

Returns
  • Array of strings naming the private files.
See Also

三、Environment中关于内存部存储的重要函数

public static File getDataDirectory ()

Since: API Level 1

Gets the Android data directory.

用File返回数据文件的根目录,返回的文件的路径为“/data”。该目录下的文件是只读。应用程序无法对该目录下的文件进行写操作。

public static File getDownloadCacheDirectory ()

Since: API Level 1

Gets the android Download/Cache content directory.

用File返回缓存文件的根目录,返回的文件的路径为“/cache”。对于第三方应用程序。该目录下的文件是只读。第三方应用程序无法对该目录下的文件进行写操作。

public static File getRootDirectory ()

API Level 1

Gets the Android root directory.

用File返回Android系统文件的根目录,返回的文件的路径为“/system”。该目录下的文件是只读。应用程序无法对该目录下的文件进行写操作。

结束

时间: 2024-08-18 04:18:44

Android中关于内部存储的一些重要函数的相关文章

彻底理解android中的内部存储与外部存储

我们先来考虑这样一个问题: 打开手机设置,选择应用管理,选择任意一个App,然后你会看到两个按钮,一个是清除缓存,另一个是清除数据,那么当我们点击清除缓存的时候清除的是哪里的数据?当我们点击清除数据的时候又是清除的哪里的数据?读完本文相信你会有答案. 在android开发中我们常常听到这样几个概念,内存,内部存储,外部存储,很多人常常将这三个东西搞混,那么我们今天就先来详细说说这三个东西是怎么回事? 内存,我们在英文中称作memory,内部存储,我们称为InternalStorage,外部存储我

Android中的内部存储与外部存储

http://www.androidchina.net/4106.html 1.内部存储 data文件夹就是我们常说的内部存储,当我们打开data文件夹之后(没有root的手机不能打开该文件夹),里边有两个文件夹值得我们关注,如下: 一个文件夹是app文件夹,还有一个文件夹就是data文件夹,app文件夹里存放着我们所有安装的app的apk文件,其实,当我们调试一个app的时候,可以看到控制台输出的内容,有一项是uploading -..就是上传我们的apk到这个文件夹,上传成功之后才开始安装.

Android SDCard和内部存储中gcc编译后的可执行文件无法运行提示 cannot execute - Permission denied

原因是mount的方式问题,root后运行 su mount -o rw,remount /mnt/sdcard   //SDCard mount -o rw,remount /mnt/internal   //内部存储,每部Android手机的内部存储路径可能不同,要写对路径 这样就可以了. 转自:http://www.cnblogs.com/ahuo/p/4111621.html

67.Android中的数据存储总结

转载:http://mp.weixin.qq.com/s?__biz=MzIzMjE1Njg4Mw==&mid=2650117688&idx=1&sn=d6c73f9f04d02ede7a741b45e801d74e#rd 本篇文章会帮助大家快速了解Android中各种数据存储机制的应用场景以及基本使用方法,力求在头脑中建立一个“目录”.有了这个目录,具体使用细节在实际应用时再查询文档即可得到. 0. 概述 Android为我们提供了以下存储机制: Shared Preferenc

Android中的数据存储

Android中的数据存储主要分为三种基本方法: 1.利用shared preferences存储一些轻量级的键值对数据. 2.传统文件系统. 3.利用SQLite的数据库管理系统. 对SharedPreferences对象和SQLite数据库,它们的数据仅对创建它们的应用是可访问的. (比如,MODE_WORLD_READABLE 和 MODE_WORLD_WRITEABLE现在(since API 17)已经被标记为deprecated). 换句话说,它们不是共享的,如果需要在不同的应用之间

Android笔记——Android中数据的存储方式(二)

我们在实际开发中,有的时候需要储存或者备份比较复杂的数据.这些数据的特点是,内容多.结构大,比如短信备份等.我们知道SharedPreferences和Files(文本文件)储存这种数据会非常的没有效率.如果学过JavaWeb的朋友,首先可能想到的是数据库.当然了数据库是一个方案,那么是否还有其他的解决方案呢?今天我们在讲下Android笔记——Android中数据的存储方式(一) 提到的除了SharedPreferences和Files(文本文件)以外的其他几种数据储存方式:xml文件.SQL

Android中的File存储

关于Android的File存储,这里先说下Java中的File类,根据面向对象的思想,Java中对文件的操作也进行了对象的封装,这个操作文件的类就是File类,File提供了丰富的api来进行文件的操作,比如常见的createNewFile(),mkdir(),mkdirs(),exists(),isFile(),isDictory(),renameto(),delete(),getName(),getPath()....方法比较多,读者可以自行通过api文档查看,同样这一套的文件操作在And

Android中的文件存储

另见Android中的文件存储 一.前言 文件存储时Android中最基本的一种数据存储方式,它不对存储的内容进行任何的格式化处理,所有数据都是原封不动地保存到文件当中的,因此它比较适合用于存储一些简单的文本数据或二进制数据. 二.将数据存储到文件中 Context类中提供了一个openFileOutput()方法,可以用于将数据存储到指定的文件中. 可以看到,此方法接收两个参数,第一个即是文件的名字,这里不可以包含文件路径,所有的文件都是默认存储到/data/data//files/目录下的.

Android中的内存储、外存储概念、文件操作与PC端的有些不同

其实安卓文件的操作和java在pc环境下的操作并无二致,之所以需要单独讲解是因为安卓系统提供了不同于pc的访问文件系统根路径的api,同时对一个应用的私有文件做了统一的管理.初学者在这部分感到很容易混淆内部存储和外部存储两个概念. 相对路径和绝对路径 在java中,关于相对路径和绝对路径是这样解释的. 绝对路径是指书写文件的完整路径,例如d:\java\Hello.java,该路径中包含文件的完整路径d:\java以及文件的全名Hello.java.使用该路径可以唯一的找到一个文件,不会产生歧义