一、简介
Android中,你也可以通过绝对路径以JAVA传统方式访问内部存储空间。但是以这种方式创建的文件是对私有,创建它的应用程序对该文件是可读可写,但是别的应用程序并不能直接访问它。不是所有的内部存储空间应用程序都可以访问,默认情况下只能访问“/data/data/你的应用程序的包名”这个路径下的文件。
注意:保存在/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.

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.
三、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”。该目录下的文件是只读。应用程序无法对该目录下的文件进行写操作。

结束

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

  1. android中的内部存储与外部存储

    我们先来考虑这样一个问题: 打开手机设置,选择应用管理,选择任意一个App,然后你会看到两个按钮,一个是清除缓存,另一个是清除数据,那么当我们点击清除缓存的时候清除的是哪里的数据?当我们点击清除数据的 ...

  2. 彻底了解android中的内部存储与外部存储

    我们先来考虑这样一个问题: 打开手机设置,选择应用管理,选择任意一个App,然后你会看到两个按钮,一个是清除缓存,另一个是清除数据,那么当我们点击清除缓存的时候清除的是哪里的数据?当我们点击清除数据的 ...

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

    我们先来考虑这样一个问题: 打开手机设置,选择应用管理,选择任意一个App,然后你会看到两个按钮,一个是清除缓存,另一个是清除数据,那么当我们点击清除缓存的时候清除的是哪里的数据?当我们点击清除数据的 ...

  4. 【转】彻底理解android中的内部存储与外部存储

    我们先来考虑这样一个问题: 打开手机设置,选择应用管理,选择任意一个App,然后你会看到两个按钮,一个是清除缓存,另一个是清除数据,那么当我们点击清除缓存的时候清除的是哪里的数据?当我们点击清除数据的 ...

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

    原因是mount的方式问题,root后运行 su mount -o rw,remount /mnt/sdcard   //SDCard mount -o rw,remount /mnt/interna ...

  6. 67.Android中的数据存储总结

    转载:http://mp.weixin.qq.com/s?__biz=MzIzMjE1Njg4Mw==&mid=2650117688&idx=1&sn=d6c73f9f04d0 ...

  7. Android笔记——Android中数据的存储方式(一)

    Android中数据的存储方式 对于开发平台来讲,如果对数据的存储有良好的支持,那么对应用程序的开发将会有很大的促进作用. 总体的来讲,数据存储方式有三种:一个是文件,一个是数据库,另一个则是网络.其 ...

  8. Android中的数据存储

    Android中的数据存储主要分为三种基本方法: 1.利用shared preferences存储一些轻量级的键值对数据. 2.传统文件系统. 3.利用SQLite的数据库管理系统. 对SharedP ...

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

    我们在实际开发中,有的时候需要储存或者备份比较复杂的数据.这些数据的特点是,内容多.结构大,比如短信备份等.我们知道SharedPreferences和Files(文本文件)储存这种数据会非常的没有效 ...

随机推荐

  1. JavaScript定时器及其他

    By Abyssly Jun 20 2014 Updated:Jun 20 2014 平时工作中不可避免地要嵌套网页,对JavaScript的深入了解还是很有必要滴.而JavaScript中一个容易让 ...

  2. php 在Linux下的安装

    1.获取php源码 wget http://cn2.php.net/get/php-5.6.6.tar.gz/from/this/mirror mv mirror php-5.6.6.tar.gz t ...

  3. webstorm_completion

    js 使用yarn 安装声明定义文件 @types/xxx koa ==> @types/koa koa-router ==> @types/koa-router 安装webstorm中的 ...

  4. Python os模块和time模块 day4

    一.os模块 print(os.listdir(r'/Users/smh/Desktop/整理'))#os.listdir() 列出某个目录下面的文件夹/文件 print(os.path.isfile ...

  5. 14.multi_match+most-fields策略

    主要知识点 most-fields策略的用法 most-fields策略和best-fields的比较         best-fields策略:将某一个field匹配尽可能多的关键词的doc优先返 ...

  6. CA认证相关

    目录 CA认证相关 基本概念 CA认证相关 公钥私钥详解>> 基本概念 密钥对: 在非对称的加密技术中心, 有两种密钥, 分为私钥和公钥,私钥 --RSA算法-->公钥. 公钥: 公 ...

  7. Codeforces Round #412 (Div. 2)ABCD

    tourist的剧毒contest,题干长到让人不想做... A.看不太懂题意直接看下面input output note n组里有两数不一样的一组就rated 否则单调不增为maybe,否则unra ...

  8. CodeForcesGym 100735H Words from cubes

    Words from cubes Time Limit: Unknown ms Memory Limit: 65536KB This problem will be judged on CodeFor ...

  9. [HEOI 2016] sort

    [HEOI 2016] sort 解题报告 码线段树快调废我了= = 其实这题貌似暴力分很足,直接$STL$的$SORT$就能$80$ 正解: 我们可以二分答案来做这道题 假设我们二分的答案为$a$, ...

  10. U - Palindrome Manacher

    Andy the smart computer science student was attending an algorithms class when the professor asked t ...