2014-12-26 51 views
2

我遇到了一個奇怪的問題,/storage/udisk/sda4//mnt/media_rw/udisk/sda4/應該是指同一個文件夾中的Android,但與下面的代碼:Android中的「/ storage/udisk/sda4 /」和「/ mnt/media_rw/udisk/sda4 /」有什麼區別?

File sdcard = new File("/mnt/media_rw/udisk/sda4"); 

Log.d(LOG_TAG, "sdcard: " + sdcard 
       + ", exists: " + sdcard.exists());   // return false 

sdcard = new File("/storage/udisk/sda4"); 

Log.d(LOG_TAG, "sdcard: " + sdcard 
       + ", exists: " + sdcard.exists());   // return true 

我得到不同的結果,這是爲什麼?

ll /mnt/media_rw/udisk/返回drwxrwx--- media_rw media_rw 1970-01-01 00:00 sda4

ll /storage/udisk返回drwxrwx--x root sdcard_r 1970-01-01 00:00 sda4

回答

0
  • 的/ mnt/media_rw /移動U盤/ SDA4安裝存儲裝置。它有更多的權限訪問(許可-771)
  • /存儲/移動U盤/ SDA4軟存儲(許可-751)

使用到/ mnt/media_rw /移動U盤/ SDA4更好訪問

+0

的路徑,你可以看到,在我的問題,我列出的權限,'的/ mnt/media_rw /移動U盤/ sda4'是'drwxrwx ---',' 770'不是'771',而'/ storage/udisk'是'drwxrwx -x','771'而不是'751' – roger

+0

mnt是用戶訪問存儲 – Elango

+0

權限不同於設備 – Elango

0

你可以試試這個代碼,我希望這方面的工作

import java.io.File; 
import java.util.ArrayList; 
import java.util.Scanner; 
import android.os.Build; 
import android.os.Environment; 
import android.util.Log; 

public class StorageOptions { 
private static ArrayList<String> mMounts = new ArrayList<String>(); 
private static ArrayList<String> mVold = new ArrayList<String>(); 

public static String[] labels; 
public static String[] paths; 
public static int count = 0; 
private static final String TAG = StorageOptions.class.getSimpleName(); 

public static void determineStorageOptions() { 
    readMountsFile(); 

    readVoldFile(); 

    compareMountsWithVold(); 

    testAndCleanMountsList(); 

    setProperties(); 
} 

private static void readMountsFile() { 
    /* 
    * Scan the /proc/mounts file and look for lines like this: 
    * /dev/block/vold/179:1 /mnt/sdcard vfat 
    * rw,dirsync,nosuid,nodev,noexec, 
    * relatime,uid=1000,gid=1015,fmask=0602,dmask 
    * =0602,allow_utime=0020,codepage 
    * =cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0 
    * 
    * When one is found, split it into its elements and then pull out the 
    * path to the that mount point and add it to the arraylist 
    */ 

    // some mount files don't list the default 
    // path first, so we add it here to 
    // ensure that it is first in our list 
    mMounts.add("/mnt/sdcard"); 

    try { 
     Scanner scanner = new Scanner(new File("/proc/mounts")); 
     while (scanner.hasNext()) { 
      String line = scanner.nextLine(); 
      if (line.startsWith("/dev/block/vold/")) { 
       String[] lineElements = line.split(" "); 
       String element = lineElements[1]; 

       // don't add the default mount path 
       // it's already in the list. 
       if (!element.equals("/mnt/sdcard")) 
        mMounts.add(element); 
      } 
     } 
    } catch (Exception e) { 
     // Auto-generated catch block 

     e.printStackTrace(); 
    } 

    for(String p:mMounts) 
    { 
     Log.i("storage", p); 
    } 
    } 

    private static void readVoldFile() { 
    /* 
    * Scan the /system/etc/vold.fstab file and look for lines like this: 
    * dev_mount sdcard /mnt/sdcard 1 
    * /devices/platform/s3c-sdhci.0/mmc_host/mmc0 
    * 
    * When one is found, split it into its elements and then pull out the 
    * path to the that mount point and add it to the arraylist 
    */ 

    // some devices are missing the vold file entirely 
    // so we add a path here to make sure the list always 
    // includes the path to the first sdcard, whether real 
    // or emulated. 
    mVold.add("/mnt/sdcard"); 

    try { 
     Scanner scanner = new Scanner(new File("/system/etc/vold.fstab")); 
     while (scanner.hasNext()) { 
      String line = scanner.nextLine(); 
      if (line.startsWith("dev_mount")) { 
       String[] lineElements = line.split(" "); 
       String element = lineElements[2]; 

       if (element.contains(":")) 
        element = element.substring(0, element.indexOf(":")); 

       // don't add the default vold path 
       // it's already in the list. 
       if (!element.equals("/mnt/sdcard")) 
        mVold.add(element); 
      } 
     } 


    } catch (Exception e) { 
     // Auto-generated catch block 

     e.printStackTrace(); 
    } 
    } 

    private static void compareMountsWithVold() { 
    /* 
    * Sometimes the two lists of mount points will be different. We only 
    * want those mount points that are in both list. 
    * 
    * Compare the two lists together and remove items that are not in both 
    * lists. 
    */ 

    for (int i = 0; i < mMounts.size(); i++) { 
     String mount = mMounts.get(i); 
     if (!mVold.contains(mount)) 
      mMounts.remove(i--); 
    } 

    // don't need this anymore, clear the vold list to reduce memory 
    // use and to prepare it for the next time it's needed. 
    mVold.clear(); 
    } 

    private static void testAndCleanMountsList() { 
    /* 
    * Now that we have a cleaned list of mount paths Test each one to make 
    * sure it's a valid and available path. If it is not, remove it from 
    * the list. 
    */ 

    for (int i = 0; i < mMounts.size(); i++) { 
     String mount = mMounts.get(i); 
     File root = new File(mount); 
     if (!root.exists() || !root.isDirectory() || !root.canWrite()) 
      mMounts.remove(i--); 
    } 
    } 

    @SuppressWarnings("unchecked") 
    private static void setProperties() { 
    /* 
    * At this point all the paths in the list should be valid. Build the 
    * public properties. 
    */ 
    Constants.mMounts = new ArrayList<String>(); 
    ArrayList<String> mLabels = new ArrayList<String>(); 

    int j = 0; 
    if (mMounts.size() > 0) { 
     if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) 
      mLabels.add("Auto"); 
     else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { 
      if (Environment.isExternalStorageRemovable()) { 
       mLabels.add("External SD Card 1"); 
       j = 1; 
      } else 
       mLabels.add("Internal Storage"); 
     } else { 
      if (!Environment.isExternalStorageRemovable() 
        || Environment.isExternalStorageEmulated()) 
       mLabels.add("Internal Storage"); 
      else { 
       mLabels.add("External SD Card 1"); 
       j = 1; 
      } 
     } 

     if (mMounts.size() > 1) { 
      for (int i = 1; i < mMounts.size(); i++) { 
       mLabels.add("External SD Card " + (i + j)); 
      } 
     } 
    } 

    labels = new String[mLabels.size()]; 
    mLabels.toArray(labels); 

    paths = new String[mMounts.size()]; 
    mMounts.toArray(paths); 
    Constants.mMounts = (ArrayList<String>) mMounts.clone(); 
    Constants.mLabels = (ArrayList<String>) mLabels.clone(); 
    count = Math.min(labels.length, paths.length); 

    // don't need this anymore, clear the mounts list to reduce memory 
    // use and to prepare it for the next time it's needed. 
    mMounts.clear(); 

    } 
} 

這個代碼可以幫助您找到所有SD卡和外部存儲路徑

我得到了我的三星手機 enter image description here

相關問題