Android系统文件结构及路径验证

@系统目录说明:

系统目录说明

1.应用程序安装目录
    1) 系统应用程序所在目录:/system/app/*.apk
    2) 用户安装应用程序所在目录:/data/app/*.apk
    3) 注意:
        a) 在模拟器中,每重启一次,/system/app/下的应用都会被还原,有时/data/app下也会被清除
        b) 若在/system/app和/data/app下有重名的应用,不一定会启动哪一个,尤其是在adb install杀死正在运行的程序时,有时旧的就会被启动
2.用户数据所在目录
        配置文件: /data/data/应用包名/shared_prefs           
        库文件所在目录:/data/data/应用包名/databases/*  
3.SD 卡对应目录:/sdcard/

@系统目录路径验证:

activity_main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <Button
        android:id="@+id/bPhoneDirectory"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="访问应用程序手机内存" />
    <Button
        android:id="@+id/bExternalStorageDirectory"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="访问SD卡" />
    <Button
        android:id="@+id/bDataDirectory"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="访问共享文件夹" />
    <Button
        android:id="@+id/bDownloadCacheDirectory"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="访问下载缓存" />
    <Button
        android:id="@+id/bRootDirectory"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="访问根目录" />
</LinearLayout>


MainActivity.java文件:

package com.example.androidstudy;

import java.io.File;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
	//定义组件
	private Button bPhoneDirectory;
	private Button bDataDirectory;
	private Button bDownloadCacheDirectory;
	private Button bExternalStorageDirectory;
	private Button bRootDirectory;
	//定义文件变量
	private File fPhoneDirectory;
	private File fDataDirectory;
	private File fDownloadCacheDirectory;
	private File fExternalStorageDirectory;
	private File fRootDirectory;
	//路径字符串
	private String name;
	/** 
	 * Called when the activity is first created. 
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//实例化组件
		bPhoneDirectory = (Button) findViewById(R.id.bPhoneDirectory);
		bRootDirectory = (Button) findViewById(R.id.bDataDirectory);
		bDownloadCacheDirectory = (Button) findViewById(R.id.bDownloadCacheDirectory);
		bExternalStorageDirectory = (Button) findViewById(R.id.bExternalStorageDirectory);
		bRootDirectory = (Button) findViewById(R.id.bRootDirectory);
		//
		bDataDirectory = (Button) findViewById(R.id.bDataDirectory);
		//返回文件系统的绝对路径
		fPhoneDirectory = this.getFilesDir();
		//获取Android的数据目录
		fDataDirectory = Environment.getDataDirectory();
		//获取Android的外部存储目录
		fDownloadCacheDirectory = Environment.getDownloadCacheDirectory();
		//获取SD文件目录
		fExternalStorageDirectory = Environment.getExternalStorageDirectory();
		//获取Android的根目录
		fRootDirectory = Environment.getRootDirectory();
		/**
		 * 没有存储卡的时候按键无效
		 */
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_REMOVED)) {
			bExternalStorageDirectory.setEnabled(false);
		}
		/**
		 * 访问应用程序手机内存
		 */
		bPhoneDirectory.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				String path = fPhoneDirectory.getPath();
				name = path + "\n";
				ListFiles(path, "");
				Toast.makeText(MainActivity.this, name, Toast.LENGTH_LONG).show();
			}
		});

		/**
		 * 访问存储卡
		 */
		bExternalStorageDirectory
				.setOnClickListener(new Button.OnClickListener() {
					public void onClick(View v) {
						String path = fExternalStorageDirectory.getPath();
						name = path + "\n";
						ListFiles(path, "");
						Toast.makeText(MainActivity.this, name, Toast.LENGTH_LONG).show();
					}
				});
		/**
		 * 
		 */
		bDownloadCacheDirectory
				.setOnClickListener(new Button.OnClickListener() {
					public void onClick(View v) {
						String path = fDownloadCacheDirectory.getAbsolutePath();
						Toast.makeText(MainActivity.this, path, Toast.LENGTH_LONG).show();
					}
				});
		/**
		 * 
		 */
		bRootDirectory.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				String path = fRootDirectory.getAbsolutePath();
				Toast.makeText(MainActivity.this, path, Toast.LENGTH_LONG).show();
			}
		});
		/**
		 * 
		 */
		bDataDirectory.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				String path = fDataDirectory.getAbsolutePath();
				Toast.makeText(MainActivity.this, path, Toast.LENGTH_LONG).show();
			}
		});

	}
	/**
	 * 
	 * @param path
	 * @param arr
	 * @return
	 */
	private boolean ListFiles(String path, String arr) {
		File file = new File(path);
		// arr为层次变量,使目录有层次感
		arr = arr + "\t"; 
		for (File f : file.listFiles()) {
			path = f.getAbsolutePath();
			name = name + arr + f.getName() + "\n";
			if (f.isDirectory() == true) {
				// 当f为文件夹的时候,进入文件夹中
				ListFiles(path, arr); 
			}
		}
		return true;
	}
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值