android獲取手機路徑
❶ Android中如何獲得外置sd卡的路徑和手機自身內存的路徑
你問的編碼?
原文:http://blog.163.com/hero_213/blog/static/39891214201162123236660/
轉載非原創。
該代碼片段可以讓我們獲取internal和external的存儲空間大小。 
import java.io.File;                                                                                                
import android.os.Environment;    
import android.os.StatFs;    
    
public class StorageUtil {
    private static final int ERROR = -1;
    /**
     * SDCARD是否存
     */
    public static boolean externalMemoryAvailable() {
        return android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED);
    }
    /**
     * 獲取手機內部剩餘存儲空間
     * @return
     */
    public static long () {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return availableBlocks * blockSize;
    }
    /**
     * 獲取手機內部總的存儲空間
     * @return
     */
    public static long getTotalInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long totalBlocks = stat.getBlockCount();
        return totalBlocks * blockSize;
    }
    /**
     * 獲取SDCARD剩餘存儲空間
     * @return
     */
    public static long () {
        if (externalMemoryAvailable()) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long availableBlocks = stat.getAvailableBlocks();
            return availableBlocks * blockSize;
        } else {
            return ERROR;
        }
    }
    /**
     * 獲取SDCARD總的存儲空間
     * @return
     */
    public static long getTotalExternalMemorySize() {
        if (externalMemoryAvailable()) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long totalBlocks = stat.getBlockCount();
            return totalBlocks * blockSize;
        } else {
            return ERROR;
        }
    }
}
1.硬體上的 block size, 應該是"sector size",linux的扇區大小是512byte
2.有文件系統的分區的block size, 是"block size",大小不一,可以用工具查看
3.沒有文件系統的分區的block size,也叫「block size」,大小指的是1024 byte
4.Kernel buffer cache 的block size, 就是"block size",大部分PC是1024
5.磁碟分區的"cylinder size",用fdisk 可以查看。
      我們這里的block size是第二種情況,一般SD卡都是fat32的文件系統,block size是4096.
      這樣就可以知道手機的內部存儲空間和sd卡存儲空間的總大小和可用大小了。
❷ 獲取android手機的自帶存儲路徑和sdcard存儲路徑
android手機獲取自帶存儲來路徑和sd卡存儲路徑的方式是:調用Environment.getExternalStorageDirectory(),返回的存儲源目錄並不是系統內置的SD卡目錄。
1.一部分手機將eMMC存儲掛載到
/mnt/external_sd
、/mnt/sdcard2
等節點知,而將外置的SD卡掛載到
Environment.getExternalStorageDirectory()這個結點。
此時,調用Environment.getExternalStorageDirectory(),則返回外置的SD的路徑。
2.而另一部分手機直接道將eMMC存儲掛載在Environment.getExternalStorageDirectory()這個節點,而將真正的外置SD卡掛載到/mnt/external_sd、/mnt/sdcard2
等節點。
此時,調用Environment.getExternalStorageDirectory(),則返回內置的SD的路徑。
❸ android 開發 獲取手機usb連接器的路徑
給你一個思路:
因為所有的外部存儲插入都會有一個系統監聽觸發,usb存儲和sd卡的監聽都是一樣的,所以可以通過監聽獲得他的路徑。這個是主要現象的代碼,你自己補充一下其他的細節。
做一個監視外界存儲設備的監聽:
        IntentFilter mIntentFilter = new IntentFilter();
        mIntentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
        registerReceiver(mountReceiver, mIntentFilter);
然後在響應里邊輸出路徑:
private BroadcastReceiver mountReceiver = new BroadcastReceiver(){
        
        public void onReceive(Context context, Intent intent){
            String path = intent.getData().getPath(); //在這里得到新插入外部存儲的路徑,sd卡或者usb存儲能獲得。
            
            if(path.startsWith(EXTERNAL_SDCARD_STORAGE) || path.startsWith(EXTERNAL_USB_STORAGE)){
              Log.e("This Log is the path for you!",path);
    };
❹ Android存儲及路徑
分為:內部存儲和外部存儲
如何區分內部存儲和外部存儲:可以從物理和邏輯區分
從物理的角度區分,內部存儲就是手機自帶存儲空間,外部存儲就是外部接入的存儲空間例如SD卡
從邏輯意義上區分,data,system 目錄就是手機的內部存儲,而 mnt 或者 storage目錄下的sdcard0指向的sdcard目錄就是外部存儲。如果是手機自帶的外部存儲被稱為機身外部存儲,外置的SD卡則稱之為外部存儲。當然兩者都稱為外部存儲也沒關系。這里描述的內部存儲和機身外部存儲都屬於機身存儲;
邏輯區分是從4.4以上版本開始的;
獲取內部存儲路徑和api對應關系
1,通過Environment
2,通過上下文Context
Build.VERSION_CODES.LOLLIPOP及以上版本新增的API
Build.VERSION_CODES.N及以上版本新增的API
特點:
1、內部存儲路徑中的文件是分類存儲的,我們無法干涉,除了cache目錄,別的目錄系統不會自動創建
2、除了files目錄,別的目錄我們幾乎都是無法手動操作的
3、別的App幾乎無法訪問內部存儲中的數據,除了用非法手段或者我們主動暴露
4、內部存儲目錄下的文件夾及文件會隨著app的卸載而被系統自動刪除
外部存儲又可分為共有目錄和私有目錄;
私有目錄
私有目錄:不需要訪問許可權
Android 在外部存儲空間中也提供了特殊目錄供App存放私有文件,該路徑為:/storage/emulated/0/Android/data/包名/
注意:應用安裝之後/storage/emulated/0/Android/data/是沒有對應的應用文件夾的,需要手動調用對應的API創建;
獲取私有目錄路徑
共有目錄
共有目錄:需要申請許可權才能訪問
許可權:6.0以上需要動態申請
獲取共有目錄的API 29中已過時:
❺ 如何正確獲得Android內外SD卡路徑
/** * 獲取手機自身內存路徑 * */ public static String getPhoneCardPath(){ return Environment.getDataDirectory().getPath(); } /** * 獲取sd卡路徑 * 雙sd卡時,根據」設置「裡面的數據存儲位置選擇,獲得的是內置sd卡或外置sd卡 * @return */ public static String getNormalSDCardPath(){ return Environment.getExternalStorageDirectory().getPath(); } /** * 獲取sd卡路徑 * 雙sd卡時,獲得的是外置sd卡 * @return */ public static String getSDCardPath() { String cmd = "cat /proc/mounts"; Runtime run = Runtime.getRuntime();// 返回與當前 Java 應用程序相關的運行時對象 BufferedInputStream in=null; BufferedReader inBr=null; try { Process p = run.exec(cmd);// 啟動另一個進程來執行命令 in = new BufferedInputStream(p.getInputStream()); inBr = new BufferedReader(new InputStreamReader(in)); String lineStr; while ((lineStr = inBr.readLine()) != null) { // 獲得命令執行後在控制台的輸出信息 Log.i("CommonUtil:getSDCardPath", lineStr); if (lineStr.contains("sdcard") && lineStr.contains(".android_secure")) { String[] strArray = lineStr.split(" "); if (strArray != null && strArray.length >= 5) { String result = strArray[1].replace("/.android_secure", ""); return result; } } // 檢查命令是否執行失敗。 if (p.waitFor() != 0 && p.exitValue() == 1) { // p.exitValue()==0表示正常結束,1:非正常結束 Log.e("CommonUtil:getSDCardPath", "命令執行失敗!"); } } } catch (Exception e) { Log.e("CommonUtil:getSDCardPath", e.toString()); //return Environment.getExternalStorageDirectory().getPath(); }finally{ try { if(in!=null){ in.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if(inBr!=null){ inBr.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return Environment.getExternalStorageDirectory().getPath(); } //查看所有的sd路徑 public String getSDCardPathEx(){ String mount = new String(); try { Runtime runtime = Runtime.getRuntime(); Process proc = runtime.exec("mount"); InputStream is = proc.getInputStream(); InputStreamReader isr = new InputStreamReader(is); String line; BufferedReader br = new BufferedReader(isr); while ((line = br.readLine()) != null) { if (line.contains("secure")) continue; if (line.contains("asec")) continue; if (line.contains("fat")) { String columns[] = line.split(" "); if (columns != null && columns.length > 1) { mount = mount.concat("*" + columns[1] + "\n"); } } else if (line.contains("fuse")) { String columns[] = line.split(" "); if (columns != null && columns.length > 1) { mount = mount.concat(columns[1] + "\n"); } } } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return mount; } //獲取當前路徑,可用空間 public static long getAvailableSize(String path){ try{ File base = new File(path); StatFs stat = new StatFs(base.getPath()); long nAvailableCount = stat.getBlockSize() * ((long) stat.getAvailableBlocks()); return nAvailableCount; }catch(Exception e){ e.printStackTrace(); } return 0; }
❻ android 怎麼獲取手機文件路徑
用 /mnt 獲取全部掛載的存儲路徑。 mnt/sdcard和mnt/sdcard1一個是系統存儲路徑一個是SD卡存儲路徑。
❼ 如何正確獲得Android內外SD卡路徑
/**
     * 獲取手機自身內存路徑
     * 
     */
public static String getPhoneCardPath(){
return Environment.getDataDirectory().getPath();
}
/**
     * 獲取sd卡路徑
     * 雙sd卡時,根據」設置「裡面的數據存儲位置選擇,獲得的是內置sd卡或外置sd卡
     * @return
     */
public static String getNormalSDCardPath(){
return Environment.getExternalStorageDirectory().getPath();
}
/**
     * 獲取sd卡路徑
     * 雙sd卡時,獲得的是外置sd卡
     * @return
     */
    public static String getSDCardPath() {
        String cmd = "cat /proc/mounts";
        Runtime run = Runtime.getRuntime();// 返回與當前 Java 應用程序相關的運行時對象
        BufferedInputStream in=null;
        BufferedReader inBr=null;
        try {
            Process p = run.exec(cmd);// 啟動另一個進程來執行命令
            in = new BufferedInputStream(p.getInputStream());
            inBr = new BufferedReader(new InputStreamReader(in));
String lineStr;
            while ((lineStr = inBr.readLine()) != null) {
                // 獲得命令執行後在控制台的輸出信息
            Log.i("CommonUtil:getSDCardPath", lineStr);
                if (lineStr.contains("sdcard")
                        && lineStr.contains(".android_secure")) {
                    String[] strArray = lineStr.split(" ");
                    if (strArray != null && strArray.length >= 5) {
                        String result = strArray[1].replace("/.android_secure",
                                "");
                        return result;
                    }
                }
                // 檢查命令是否執行失敗。
                if (p.waitFor() != 0 && p.exitValue() == 1) {
                    // p.exitValue()==0表示正常結束,1:非正常結束
                Log.e("CommonUtil:getSDCardPath", "命令執行失敗!");
                }
            }
        } catch (Exception e) {
            Log.e("CommonUtil:getSDCardPath", e.toString());
            //return Environment.getExternalStorageDirectory().getPath();
        }finally{
            try {
            if(in!=null){
                    in.close();
            }
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
            try {
            if(inBr!=null){
    inBr.close();
            }
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
        }
        return Environment.getExternalStorageDirectory().getPath();
    }
    //查看所有的sd路徑
    public String getSDCardPathEx(){
String mount = new String();
try {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("mount");
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
String line;
BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
if (line.contains("secure")) continue;
if (line.contains("asec")) continue;
if (line.contains("fat")) {
String columns[] = line.split(" ");
if (columns != null && columns.length > 1) {
mount = mount.concat("*" + columns[1] + "\n");
}
} else if (line.contains("fuse")) {
String columns[] = line.split(" ");
if (columns != null && columns.length > 1) {
mount = mount.concat(columns[1] + "\n");
}
}
} 
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mount;
    }
    //獲取當前路徑,可用空間
    public static long getAvailableSize(String path){
    try{
        File base = new File(path);
    StatFs stat = new StatFs(base.getPath());
    long nAvailableCount = stat.getBlockSize() * ((long) stat.getAvailableBlocks());
    return nAvailableCount;
    }catch(Exception e){
    e.printStackTrace();
    }
    return 0;
    }
❽ 獲取android手機的自帶存儲路徑和sdcard存儲路徑
android手機獲取自帶存儲路徑和sd卡存儲路徑的方式是:調用Environment.getExternalStorageDirectory(),返回的存儲目錄並不是系統內置的SD卡目錄。
1.一部分手機將eMMC存儲掛載到
/mnt/external_sd
、/mnt/sdcard2
等節點,而將外置的SD卡掛載到
Environment.getExternalStorageDirectory()這個結點。
此時,調用Environment.getExternalStorageDirectory(),則返回外置的SD的路徑。
2.而另一部分手機直接將eMMC存儲掛載在Environment.getExternalStorageDirectory()這個節點,而將真正的外置SD卡掛載到/mnt/external_sd、/mnt/sdcard2
等節點。
此時,調用Environment.getExternalStorageDirectory(),則返回內置的SD的路徑。
❾ 各類Android手機的根目錄如何獲取
可以使用Android原生的的類Environment.getExternalStorageDirectory()來獲取,一般用「/sdcard/」是可以獲取大部分的手機內存的根目錄,但是現在好像陸續的不推薦這樣去做,而是用Android原生的方法。有一個前提是你必須加入讀寫許可權才可以進行此操作,否則無效
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
記得在清單文件中加上
❿ Android 獲取手機內部存儲的路徑
方法有許多··
推薦: 用電腦開,方法如下電腦手機都安裝騰訊 應用寶軟體   然後手機連接電腦   打開電腦上的應用寶 可以直接 查看手機內存路徑信息
其他辦法:安裝第三方系統工具,可以直接查看內存信息,這個方法可以取眾多的安卓下載平台獲取
