我是靠谱客的博主 诚心眼神,这篇文章主要介绍Android之常用文件路径Android常用文件路径,现在分享给大家,希望可以做个参考。

Android常用文件路径

一、系统路径

1.获取系统路径:

复制代码
1
2
3
4
/* 取得system的路径为:/system */ // File rootFile = Environment.getRootDirectory(); // String rootPath = Environment.getRootDirectory().getAbsolutePath();

2.获取data路径:

复制代码
1
2
3
4
/* 取得data的路径为:/data */ // File dataFile = Environment.getDataDirectory(); // String dataPath = Environment.getDataDirectory().getAbsolutePath();

3.获取应用程序安装路径:

复制代码
1
2
3
4
/* 取得apk的路径为:/data/app/com.yangliu.testsqlite-1.apk */ String packResPath=getApplicationContext().getPackageResourcePath();// getApplicationContext()即为mContext /* 取得apk的路径为:/data/app/com.yangliu.testsqlite-1.apk */ String packCodePath=getApplicationContext().getPackageCodePath();

4.取得应用程序私有路径:

复制代码
1
2
3
4
5
6
7
8
9
/* 取得cache的路径为:/data/data/com.yangliu.testsqlite/cache */ File cacheFile = getApplicationContext().getCacheDir();// 等同如下 String cachePath = getApplicationContext().getCacheDir().getAbsolutePath(); /* 取得files的路径为:/data/data/com.yangliu.testsqlite/files */ File filesFile = getApplicationContext().getFilesDir(); String filesPath = getApplicationContext().getFilesDir().getAbsolutePath(); /* 取得databases下指定数据库的路径为:/data/data/com.yangliu.testsqlite/databases/basechannel.db */ File databaseFile = getApplicationContext().getDatabasePath("basechannel.db"); String databasePath = getApplicationContext().getDatabasePath("basechannel.db").getAbsolutePath();

5.应用下files文件使用:

复制代码
1
2
3
4
5
6
7
8
9
/* 返回/data/data/youPackageName/files的File对象 */ File filesFile = getApplicationContext().getFilesDir(); /* 得到files下文件的FileInputStream流和FileOutputStream流对象 */ FileInputStream textFIS = getApplicationContext().openFileInput("text.txt"); FileOutputStream textFOS = getApplicationContext().openFileOutput("text.txt"); /* 得到files下所有的文件名,返回的是String[]对象 */ String[] fileNames = getApplicationContext().fileList(); /* 删除files下指定名称的文件 */ getApplicationContext().deleteFile("text.txt");

6.取得指定文件路径:

复制代码
1
2
/* 得到/data/data/youPackageName/下的指定名称的文件夹File对象,如果该文件夹不存在则用指定名称创建一个新的文件夹 */ // getApplicationContext().getDir(String name, int mode);

二、代码路径

1.取得assets文件路径:

复制代码
1
2
/* 取得assets的路径,指定绝对路径 */ String assetsPath=String path = "file:///android_asset/文件名";
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
/* 取得assets的路径,以流的方式读取 */ InputStream assetsIS = getClass().getResourceAsStream("/assets/文件名"); String assetsPath= new String(InputStreamToByte(assetsIS));// private byte[] InputStreamToByte(InputStream is) throws IOException { ByteArrayOutputStream bytestream = new ByteArrayOutputStream(); int ch; while ((ch = is.read()) != -1) { bytestream.write(ch); } byte imgdata[] = bytestream.toByteArray(); bytestream.close(); return imgdata; }

最后

以上就是诚心眼神最近收集整理的关于Android之常用文件路径Android常用文件路径的全部内容,更多相关Android之常用文件路径Android常用文件路径内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(63)

评论列表共有 0 条评论

立即
投稿
返回
顶部