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卡里的目錄了。