android获取当前目录
❶ android 已知文件名,怎么取得目录
获得目录 = getFilePathByName(已经知道的文件名,根目录);
/**
*
* @param seekFileName 需要查找的File名
* @param rootFile 在哪里查找的目录
* @return 查找文件的全路径
*/
private String getFilePathByName(String seekFileName,File rootFile){
List<File> files=parseFiles(rootFile);
for (File file:files){
if(file.getName().equals(seekFileName)){
return file.getAbsolutePath();
}
}
return null;
}
private List<File> parseFiles(File file){
List<File> listFiles=new ArrayList<>();
File[] files = file.listFiles();
for (File mf:files){
if(mf.isDirectory()){
listFiles.addAll(parseFiles(mf));
}else{
listFiles.add(mf);
}
}
return listFiles;
}
❷ 获取android手机的自带存储路径和sdcard存储路径
android手机获取自带存储路径和sd卡存储路径的方式是:
调用Environment.getExternalStorageDirectory,返回的存储目录并不是系统内置的SD卡目录。
1、手机将eMC存储挂载到/mnt/external_sd、/mnt/sdcard2等节点,而将外置的SD卡挂载到Environment.getExternalStorgeDirectory这个结点。
2、而另一部分手机直接将eMMC存储挂载在Environment.getExternalStorageDirectory这个节点,而将真正的外置SD卡挂载到/mnt/external_sd、/mnt/sdcard2等节点。调用Environment.getExternalStorgeDirectory,则返回内置的SD的路径。
(2)android获取当前目录扩展阅读:
Android在正式发行之前,最开始拥有两个内部测试版本,并且以着名的机器人名称来对其进行命名,它们分别是:阿童木(AndroidBeta),发条机器人(Android1.0)。后来由于涉及到版权问题,谷歌将其命名规则变更为用甜点作为它们系统版本的代号的命名方法。
Android的系统架构和其操作系统一样,采用了分层的架构。从架构图看,Android分为四个层,从高层到低层分别是应用程序层、应用程序框架层、系统运行库层和linux内核层。
❸ Android如何获取asset目录下所有文件的路径
package com.hangcheng.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
public class GetfilesFromAssets {
private Activity activity;
private String[] files;
public GetfilesFromAssets(Context context) {
this.activity = (Activity) context;
}
// public void deepFile(Context ctxDealFile, String path) {
// try {
// String str[] = ctxDealFile.getAssets().list(path);
// if (str.length > 0) {// 如果是目录
// File file = new File("/data/" + path);
// file.mkdirs();
// for (String string : str) {
// path = path + "/" + string;
// System.out.println("zhoulc:\t" + path);
// // textView.setText(textView.getText()+"\t"+path+"\t");
// deepFile(ctxDealFile, path);
// path = path.substring(0, path.lastIndexOf('/'));
// }
// } else {// 如果是文件
// InputStream is = ctxDealFile.getAssets().open(path);
// FileOutputStream fos = new FileOutputStream(new File("/data/"
// + path));
// byte[] buffer = new byte[1024];
// int count = 0;
// while (true) {
// count++;
// int len = is.read(buffer);
// if (len == -1) {
// break;
// }
// fos.write(buffer, 0, len);
// }
// is.close();
// fos.close();
// }
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
public String[] getfileFromAssets(String path) {
AssetManager assetManager = activity.getAssets();
// String[] files;
try {
files = assetManager.list(path);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return files;
}
public List listHtmlOfAssets() {
List list = new ArrayList();
files = getfileFromAssets("html");
for (int i = 0; i < files.length; i++) {
HashMap map = new HashMap();
map.put("htmlname", files[i]);
list.add(map);
}
return list;
}
}
❹ android获取应用安装之后的路径
Log.d(TAG," this.getFilesDir().getParent()"+this.getFilesDir().getParent());
this.getFilesDir().getParent()/data/user/0/com.xxx.xxx
com.xxx.xxx为该应用的包名
以后就不用另行去判断是真机还是模拟器的安装之后文件的路径了
该方法直接返回安装之后包所在的路径
手机无法直接查看应用安装之后 应用宝所在的具体路径的 可以root之后 或者开发工具Android Studio点击Device File Explorer等方法查看
❺ android中怎么获取指定目录下的文件夹
参考如下代码:
package com.Aina.Android;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class Test_ListFile extends ListActivity {
/** Called when the activity is first created. */
private List<String> items = null;//存放名称
private List<String> paths = null;//存放路径
private String rootPath = "/";
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) this.findViewById(R.id.TextView);
this.getFileDir(rootPath);//获取rootPath目录下的文件.
}
public void getFileDir(String filePath) {
try{
this.tv.setText("当前路径:"+filePath);// 设置当前所在路径
items = new ArrayList<String>();
paths = new ArrayList<String>();
File f = new File(filePath);
File[] files = f.listFiles();// 列出所有文件
// 如果不是根目录,则列出返回根目录和上一目录选项
if (!filePath.equals(rootPath)) {
items.add("返回根目录");
paths.add(rootPath);
items.add("返回上一层目录");
paths.add(f.getParent());
}
// 将所有文件存入list中
if(files != null){
int count = files.length;// 文件个数
for (int i = 0; i < count; i++) {
File file = files[i];
items.add(file.getName());
paths.add(file.getPath());
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
this.setListAdapter(adapter);
}catch(Exception ex){
ex.printStackTrace();
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String path = paths.get(position);
File file = new File(path);
//如果是文件夹就继续分解
if(file.isDirectory()){
this.getFileDir(path);
}else{
new AlertDialog.Builder(this).setTitle("提示").setMessage(file.getName()+" 是一个文件!").setPositiveButton("OK", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
}
}
❻ 各类Android手机的根目录如何获取
可以使用Android原生的的类Environment.getExternalStorageDirectory()来获取,一般用“/sdcard/”是可以获取大部分的手机内存的根目录,但是现在好像陆续的不推荐这样去做,而是用Android原生的方法。有一个前提是你必须加入读写权限才可以进行此操作,否则无效
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
记得在清单文件中加上
❼ android怎样拿到file的路径
Environment 常用方法:
* 方法:getDataDirectory()
解释:返回 File ,获取 Android
数据目录。
* 方法:getDownloadCacheDirectory()
解释:返回 File ,获取 Android
下载/缓存内容目录。
* 方法:getExternalStorageDirectory()
解释:返回 File ,获取外部存储目录即
SDCard
* 方法:(String type)
解释:返回 File
,取一个高端的公用的外部存储器目录来摆放某些类型的文件
* 方法:getExternalStorageState()
解释:返回 File
,获取外部存储设备的当前状态
* 方法:getRootDirectory()
解释:返回 File ,获取 Android 的根目录
file的getPath getAbsolutePath和getCanonicalPath的不同
File的这三个方法在api中都有说明,仅以程序为例说明。
package test;
import
java.io.File;
import java.io.IOException;
public class TestFilePath
{
public static void main(String[] args) {
// TODO Auto-generated
methodstub
System.out.println(System.getProperty("user.dir"));
try
{
System.out.println("-----默认相对路径:取得路径不同------");
File file1 =new
File("..\\src\\test1.txt");
System.out.println(file1.getPath());
System.out.println(file1.getAbsolutePath());
System.out.println(file1.getCanonicalPath());
System.out.println("-----默认相对路径:取得路径不同------");
File
file =new
File(".\\test1.txt");
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());
System.out.println(file.getCanonicalPath());
System.out.println("-----默认绝对路径:取得路径相同------");
File
file2 =new
File("D:\\workspace\\test\\test1.txt");
System.out.println(file2.getPath());
System.out.println(file2.getAbsolutePath());
System.out.println(file2.getCanonicalPath());
}
catch (IOException e) {
// TODOAuto-generated catch
block
e.printStackTrace();
}
}
}
程序执行结果如下:
F:\eclipseworkspace\testejb
-----默认相对路径:取得路径不同------
..\src\test1.txt
F:\eclipseworkspace\testejb\..\src\test1.txt
F:\eclipseworkspace\src\test1.txt
-----默认相对路径:取得路径不同------
.\test1.txt
F:\eclipseworkspace\testejb\.\test1.txt
F:\eclipseworkspace\testejb\test1.txt
-----默认绝对路径:取得路径相同------
D:\workspace\test\test1.txt
D:\workspace\test\test1.txt
D:\workspace\test\test1.txt
结论:
当输入为绝对路径时,返回的都是绝对路径。
当输入为相对路径时:
getPath()返回的是File构造方法里的路径,是什么就是什么,不增不减
getAbsolutePath()返回的其实是user.dir+getPath()的内容,从上面F:\eclipseworkspace\testejb、F:\eclipseworkspace\testejb\..\src\test1.txt、F:\eclipseworkspace\testejb\.\test1.txt可以得出。
getCanonicalPath()返回的就是标准的将符号完全解析的路径
public String
getAbsolutePath()返回抽象路径名的绝对路径名字符串。
如果此抽象路径名已经是绝对路径名,则返回该路径名字符串,这与 getPath()
方法一样。如果此抽象路径名是空的抽象路径名,则返回当前用户目录的路径名字符串,该目录由系统属性 user.dir
指定。否则,使用与系统有关的方式分析此路径名。在 UNIX 系统上,通过根据当前用户目录分析某一相对路径名,可使该路径名成为绝对路径名。在 Microsoft
Windows
系统上,通过由路径名指定的当前驱动器目录(如果有)来分析某一相对路径名,可使该路径名成为绝对路径名;否则,可以根据当前用户目录来分析它。
返回:
绝对路径名字符串,它与此抽象路径名表示相同的文件或目录的
抛出:
SecurityException
- 如果无法访问所需的系统属性值。
另请参见:
isAbsolute()
public String getCanonicalPath()
throws
IOException返回抽象路径名的规范路径名字符串。
规范路径名是绝对路径名,并且是惟一的。规范路径名的准确定义与系统有关。如有必要,此方法首先将路径名转换成绝对路径名,这与调用
getAbsolutePath() 方法的效果一样,然后用与系统相关的方式将它映射到其惟一路径名。这通常涉及到从路径名中移除多余的名称(比如 "." 和
"..")、分析符号连接(对于 UNIX 平台),以及将驱动器名转换成标准大小写形式(对于 Microsoft Windows
平台)。
表示现有文件或目录的每个路径名都有一个惟一的规范形式。表示非存在文件或目录的每个路径名也有一个惟一的规范形式。非存在文件或目录路径名的规范形式可能不同于创建文件或目录之后同一路径名的规范形式。同样,现有文件或目录路径名的规范形式可能不同于删除文件或目录之后同一路径名的规范形式。
返回:
表示与此抽象路径名相同的文件或目录的规范路径名字符串
抛出:
IOException
- 如果发生 I/O 错误(可能是因为构造规范路径名需要进行文件系统查询)
SecurityException -
如果无法访问所需的系统属性值,或者存在安全管理器,且其 SecurityManager.checkRead(java.io.FileDescriptor)
方法拒绝对该文件进行读取访问
从以下版本开始:
JDK1.1
❽ Android:怎么获取当前文件(类)所在的目录的完整路径
android的硬存储是按照linux系统规则来划分的,用于存储客户信息和客户安装的应用的是/data分区,你如果使用的是c程序的话,应该是在/data/app之类的下面,我手头没有板子没法给你查。文件夹是google约定俗成的,不同厂家应该都是一样的路径。如果是厂家定制的应用则是在/system分区下,路径应该是/system/app下。
如果你是一个android系统学习者,而不是应用开发者的话,我建议看看以下system/etc/initrc对于你会有很大的帮助。
❾ android怎么获sd卡内的所有文件目录
android程序获取SD卡目录的方法如下:
手机通过数据线连接电脑端,在计算机里会显示两个磁盘,一个是系统内存,另外一个就是SD卡内存,打开SD卡就可以找到文件目录。
也可以通过手机查找,打开手机应用程序,点击文件管理打开,然后打开所有文件。
接着打开”extSdCard“文件夹就是SD卡里的目录了。