`

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

阅读更多
通过下面这个例子我们可以了解存储卡,缓存等文件路径
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>


分享到:
评论

相关推荐

    Android---课程设计

    说明: 测试手机为小米2s,能够完整采集内置加速度传感器数据。 功能: 1.实时显示加速度数据、方向数据 2.计算收集时长(单位:ms) 3.连续计时(允许中断) 4.秒表 5.启动/关闭键(绿表停,红表运行) ...4.文件存储

    手机 pdf 阅读器

    阅读设置中增加一个“文件缓存”选项,默认情况下是开启的,在NOKIA手机上会提高UMD等文件的表现,但由于测试并不充足,如果程序经常在阅读时出错,请关闭该选项(其它手机是否开启该选项并无明显的影响) ...

    java源码包2

    2个目标文件,FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户...

    Android代码-superFileView

    &gt; * 支持测试本demo之前先把test文件夹里的文件复制到手机存储,方便测试,路径如下: TBS服务接入 参考腾讯TBS官网,地址:https://x5.tencent.com/tbs/guide/sdkInit.html 效果图 常见问题 no suport by 出现...

    JAVA上百实例源码以及开源项目

    2个目标文件,FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户...

    FTP_Server-Multiple_FTP_users_v0.13.1_b131.apk

    个功能强大的应用程序可让您在手机上运行FTP 服务器,并帮助您的朋友或您通过Internet访问/共享文件。它还可以帮助您不要使用设备的USB端口来延长设备的使用寿命。也称为Wifi文件传输或无线文件管理。 应用程序功能 ...

    基于uniapp+springboot实现的声纹考勤系统app设计毕业源代码+文档说明+sql文件

    手机端接口都在web.controller.api.SysApiController里面,录音文件上传接口是在cn.ttitcn.web.controller.common.CommonController里面的common/uploadVoice,他的返回值是该录音文件在服务器硬盘中存储的绝对路径...

    java源码包---java 源码 大量 实例

    2个目标文件,FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户...

    Qualcomm平台BREW开发技术文档

    认识手机的的存储区间 11  手机系统的组成 11  NOR 11  RAM 12  NAND 12  什么是BootLoader? 12  BootLoader 12  引导系统启动 13  下载BIN文件 13  关机充电 13  如何下载BootLoader 13  ...

    QUALCOMM平台BUIW开发文档

     文件路径错误。 46  数据缓冲区内存分配太小。 46  系统内部解析错误。 47 程序CLSID规范 48  CLSID是什么? 48  CLSID的定义 48  CLSID和BID文件的位置 48  应用引用CLSID 49  CLSID错误的做法 49...

    java源码包3

    2个目标文件,FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户...

    java源码包4

    2个目标文件,FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户...

    成百上千个Java 源码DEMO 4(1-4是独立压缩包)

    FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户可以在终端上...

    成百上千个Java 源码DEMO 3(1-4是独立压缩包)

    FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户可以在终端上...

    TBSViewer:腾讯TBS服务简单封装,实现文档预览功能

    支持测试本demo之前先把test文件夹里的文件复制到手机存储,方便测试,路径如下: TBS服务接入 参考腾讯TBS官网,地址: 效果图 使用方式 allprojects { repositories { maven { url 'https://jitpack.io' } ...

    JAVA上百实例源码以及开源项目源代码

    2个目标文件,FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户...

    android开发实例大全_王东华

    实例034:加载手机磁盘中的文件 73 实例035:实现抽屉样式效果 76 实例036:在屏幕中动态显示多种对话框 82 实例037:手机秘书--重要事情提醒 88 实例038:动态添加或删除菜单 96 实例039: 自定义Android控件实现...

    PHPCMS V9.6.6 修改版

    thumb(图片id或者路径, 宽度, 高度, 是否水印, 缩放标准值, 是否下载远程图片进行剪切) 缩略图地址:{thumb($字段名, 100, 100)} 缩略图带上水印地址:{thumb($字段名, 100, 100, 1)} 强制高度、强制宽度:{thumb($...

Global site tag (gtag.js) - Google Analytics