手机各种文件存储路径测试

通过下面这个例子我们可以了解存储卡,缓存等文件路径
1. DisplayFiles.java文件

package cn.com;

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 DisplayFiles<bRootDirectory> 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.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();
fDataDirectory = Environment.getDataDirectory();
fDownloadCacheDirectory = Environment.getDownloadCacheDirectory();
fExternalStorageDirectory = Environment.getExternalStorageDirectory();
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(DisplayFiles.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(DisplayFiles.this, name,
Toast.LENGTH_LONG).show();
}
});

bDownloadCacheDirectory
.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
String path = fDownloadCacheDirectory.getAbsolutePath();
Toast.makeText(DisplayFiles.this, path,
Toast.LENGTH_LONG).show();
}
});

bRootDirectory.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
String path = fRootDirectory.getAbsolutePath();
Toast.makeText(DisplayFiles.this, path, Toast.LENGTH_LONG)
.show();
}
});

bDataDirectory.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
String path = fDataDirectory.getAbsolutePath();
Toast.makeText(DisplayFiles.this, path, Toast.LENGTH_LONG)
.show();
}
});

}

private boolean ListFiles(String path, String arr) {
File file = new File(path);
arr = arr + "\t"; // arr为层次变量,使目录有层次感

for (File f : file.listFiles()) {
path = f.getAbsolutePath();
name = name + arr + f.getName() + "\n";

if (f.isDirectory() == true) {
ListFiles(path, arr); // 当f为文件夹的时候,进入文件夹中
}
}

return true;
}
}

2.main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />

<Button android:id="@+id/bPhoneDirectory" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/bPhoneDirectory" />
<Button android:id="@+id/bExternalStorageDirectory"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="@string/bExternalStorageDirectory" />

<Button android:id="@+id/bDataDirectory" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/bDataDirectory" />
<Button android:id="@+id/bDownloadCacheDirectory"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="@string/bDownloadCacheDirectory" />
<Button android:id="@+id/bRootDirectory" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/bRootDirectory" />
</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值