android 文件系统分析

first step:

    explain form sdk doc

//------------

Saving cache files

If you're using API Level 8 or greater, use getExternalCacheDir() to open aFile that represents theexternal storage directory where you should save cache files. If the user uninstalls yourapplication, these files will be automatically deleted. However, during the life of yourapplication, you should manage these cache files and remove those that aren't needed in order topreserve file space.

If you're using API Level 7 or lower, use getExternalStorageDirectory() to open aFile that representsthe root of the external storage, then write your cache data in the following directory:

/Android/data/<package_name>/cache/

The <package_name> is your Java-style package name, such as "com.example.android.app".


//-------------

Saving cache files

If you'd like to cache some data, rather than store it persistently, you should usegetCacheDir() to open aFile that represents the internal directory where your application should savetemporary cache files.

When the device islow on internal storage space, Android may delete these cache files to recover space. However, youshould not rely on the system to clean up these files for you. You should always maintain the cachefiles yourself and stay within a reasonable limit of space consumed, such as 1MB. When the useruninstalls your application, these files are removed.

Other useful methods

getFilesDir()
Gets the absolute path to the filesystem directory where your internal files are saved.
getDir()
Creates (or opens an existing) directory within your internal storage space.
deleteFile()
Deletes a file saved on the internal storage.
fileList()

Returns an array of files currently saved by your application.


second step 2:

       elong  项目实战,封装的方法

        

	public void writeBitmap2Internal(String filename,Bitmap bitmap){
		try {
			if (!TextUtils.isEmpty(filename) && bitmap != null && Utils.getTopActivity() != null) {
				//--write data into file--
				FileOutputStream fos = Utils.getTopActivity().openFileOutput(filename,Context.MODE_PRIVATE);
				bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
				fos.flush();
				fos.close();
			}
		} catch (FileNotFoundException e) {
			if(IConfig.DEBUG_ON) Debug.ue(TAG, "writeBitmap2Internal() exc = e"+e.toString());
		} catch (IOException e) {
			if(IConfig.DEBUG_ON) Debug.ue(TAG, "writeBitmap2Internal() exc = e"+e.toString());
		}
	}
	
	public Bitmap readBitmapFromInternal(String filename){
		try {
			if(Utils.getTopActivity() !=null){
				File bitmapfile = new File(Utils.getTopActivity().getFilesDir(),filename);
				
				if(bitmapfile.exists()){
					BitmapDrawable bitmapd = null;
					bitmapd = (BitmapDrawable) BitmapDrawable.createFromPath(bitmapfile.toString());
					
					return bitmapd.getBitmap();
				}
			}
		} catch (Exception e) {
			if(IConfig.DEBUG_ON) Debug.ue(TAG, "readBitmapFromInternal() exc = e"+e.toString());
		}
		
		return null;
	}


step 3 网友整理的资源


来 源 : 安卓网    作 者 : Tekkaman-Itachi

经过这几天在网上搜索关于Android文件系统的相关资料,发现已经有很多高手分析了Android的文件系统,这些高手多从不同的方面分析了其文件系统的结构和运行原理。我这里就写一下自己的理解。
首先是Android的文件系统根目录的结构:

所谓大成者,不拘泥于小节.    文件系统,多简洁的描述

http://www.cnblogs.com/leaven/archive/2010/04/20/1716317.html

# pwd && ls -a -l

/
drwxrwxrwt root     root                    2009-06-10 09:53 sqlite_stmt_journals
drwxrwx--- system   cache             2008-09-06 22:51 cache
d---rwxrwx system   system           1970-01-01 08:00 sdcard
lrwxrwxrwx root     root                     2009-06-09 22:11 etc -> /system/etc
drwxr-xr-x root     root                       2008-09-06 22:45 system
drwxr-xr-x root     root                       1970-01-01 08:00 sys
drwxr-x--- root     root                       1970-01-01 08:00 sbin
-rw-r--r-- root     root                         117 1970-01-01 08:00 runme.sh
dr-xr-xr-x root     root                        1970-01-01 08:00 proc
-rwxr-x--- root     root                        1704 1970-01-01 08:00 init.trout.rc
-rwxr-x--- root     root                        9329 1970-01-01 08:00 init.rc
-rwxr-x--- root     root                       1677 1970-01-01 08:00 init.goldfish.rc
-rwxr-x--- root     root                       106636 1970-01-01 08:00 init
-rw-r--r-- root     root                       118 1970-01-01 08:00 default.prop
drwxrwx--x system   system         2008-09-06 22:51 data
drwx------ root     root                      2009-06-07 16:29 root
drwxr-xr-x root     root                     2009-06-09 22:11 dev
sqlite_stmt_journals:一个根目录下的tmpfs文件系统,用于存放临时文件数据。
cache : 是缓存临时文件夹,据说是除了T-mobile的OTA更新外,别无用处。
sdcard:是SD卡中的FAT32文件系统挂载的目录
etc  :指向 /system/etc ,众所周知的配置文件存放目录
system :是一个很重要的目录,系统中的大部分东西都在这里了,以下是目录结构:
# ls -a -l /system
drwxr-xr-x root     208               1970-01-01 08:00 xbin
drwxr-xr-x root     root              1970-01-01 08:00 modules
drwxr-xr-x root     root              2008-08-01 20:00 framework
drwxr-xr-x root     root              2008-08-01 20:00 fonts
drwxr-xr-x root     root              2008-08-01 20:00 etc
-rw-r--r-- root     root                2197 2008-08-01 20:00 build.prop
drwxr-xr-x root     root              2008-08-01 20:00 media
drwxr-xr-x root     shell             2008-08-01 20:00 bin
drwxr-xr-x root     root              2008-08-01 20:00 usr
drwxr-xr-x root     root              2008-08-01 20:00 app
drwxr-xr-x root     root              2008-09-06 22:45 lost+found
drwxr-xr-x root     root              2008-08-01 20:00 lib
drwxr-xr-x root     root              2008-08-01 20:00 sd
-rw-r--r-- root     root               145 2008-08-01 20:00 init.rc


sys :用于挂载 sysfs文件系统。 在设备模型中,sysfs文件系统用来表示设备的结构.将设备的层次结构形象的反应到用户空间中.用户空间可以修改sysfs中的文件属性来修改设备的属性值 
sbin: 只放了一个用于调试的adbd程序。
proc :/proc 文件系统下的多种文件提供的系统信息不是针对某个特定进程的,而是能够在整个系统范围的上下文中使用。
data :存放用户安装的软件以及各种数据。
root :什么都没有。
dev :不用多说了,设备节点文件的存放地。
下面介绍非目录的文件:

runme.sh用于 SD 卡中 EXT2文件系统的自动挂载动作的脚本。
init.trout.rc,init.rc,init.goldfish.rc 是初始化文件.
init是系统启动到文件系统的时候第一个运行的程序。
从以上的根目录分析来看,Android的根文件系统并非标准的Linux文件系统,所以以后还得仔细分析一下启动过程,才能认识Android系统。

  /system目录是在Android文件系统占有及其重要的位置,基本上所有的工具和应用程序都在这个目录下,我看来是一个真正的rootfs。他在Android手机中存放在nandflash的mtd3中,是一个yaffs2文件系统,在启动时被挂载在root的/system目录下,其中包含有:

# pwd && ls -a -l 
/system
drwxr-xr-x root     208               1970-01-01 08:00 xbin
drwxr-xr-x root     root              1970-01-01 08:00 modules
drwxr-xr-x root     root              2008-08-01 20:00 framework
drwxr-xr-x root     root              2008-08-01 20:00 fonts
drwxr-xr-x root     root              2008-08-01 20:00 etc
-rw-r--r-- root     root                2197 2008-08-01 20:00 build.prop
drwxr-xr-x root     root              2008-08-01 20:00 media
drwxr-xr-x root     shell             2008-08-01 20:00 bin
drwxr-xr-x root     root              2008-08-01 20:00 usr
drwxr-xr-x root     root              2008-08-01 20:00 app
drwxr-xr-x root     root              2008-09-06 22:45 lost+found
drwxr-xr-x root     root              2008-08-01 20:00 lib
drwxr-xr-x root     root              2008-08-01 20:00 sd
-rw-r--r-- root     root               145 2008-08-01 20:00 init.rc
下面逐个分析其中的目录:
xbin               :下放了很多系统管理工具,这些工具不是到toolbox的链接,每个都是可执行程序。如果你看到这些命令你会发现他们根本不常用,他们都是为系统管理员准备的,是一些系统管理和配置工具。这个文件夹的作用相当于标准Linux文件系统中的/sbin。我的手机此目录下有busybox,肯定是改过的,应该是破解者加上的。
modules :使用来存放内核模块(主要是fs和net)和模块配置文件的地方。
framework : 是JAVA平台的一些核心文件,属于JAVA平台系统框架文件。里面的文件都是.jar和.odex文件。
                       备注:什么是odex文件? odex是被优化过的JAVA程序文件,体积通常是.jar的4倍左右。执行效率比.jar高。
fonts             :很显然,这是字体库文件的存放目录。
etc                 :这里存放了系统中几乎所有的配置文件,根目录下的/etc就链结于此。
build.prop    :是一个属性文件,在Android系统中.prop文件很重要,记录了系统的设置和改变,类似于/etc中的文件。
media           :里面主要是存放了系统的铃声的,分为 notifications(通知)、ui(界面)、alarms(警告)和ringtones(铃声),里面都是.ogg音频文件。
bin                 :众所周知,是存放用户常用的工具程序的,其中大部分是到toolbox的链接(类似嵌入式Linux中的busybox)。toolbox应该是google简化版的busybox,我还没深入研究过。
usr                :用户的配置文件,如键盘布局、共享、时区文件等等。您可以cat 来看看。
app               :顾名思义,存放的是Android系统自带的JAVA应用程序。
lost+found  :yaffs文件系统固有的,类似回收站的文件夹,只有是yaffs文件系统都会有。
lib                  :存放几乎所有的共享库(.so)文件。
sd                  :SD卡中的EXT2分区的挂载目录
init.rc            :一个初始化脚本,用于将/system/modules和/system/xbin挂载为cramfs,避免系统被无意破坏。
好了,/system目录的结构的结构就简单分析到这里.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值