“与”的区别是什么/存储/udisk/sda4/";及/mnt/media“rw/udisk/sda4/”;在Android中?
我遇到了一个奇怪的问题,“与”的区别是什么/存储/udisk/sda4/";及/mnt/media“rw/udisk/sda4/”;在Android中?,android,file,sd-card,android-sdcard,mount,Android,File,Sd Card,Android Sdcard,Mount,我遇到了一个奇怪的问题,/storage/udisk/sda4/和/mnt/media\u rw/udisk/sda4/应该表示android中的同一文件夹,但代码如下: File sdcard = new File("/mnt/media_rw/udisk/sda4"); Log.d(LOG_TAG, "sdcard: " + sdcard + ", exists: " + sdcard.exists()); // return fal
/storage/udisk/sda4/
和/mnt/media\u rw/udisk/sda4/
应该表示android中的同一文件夹,但代码如下:
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\u rw/udisk/
returndrwxrwx---media\u rw media\u rw 1970-01-01 00:00 sda4
ll/storage/udisk
returndrwxrwx--x根SD卡\u r 1970-01-01 00:00 sda4
使用/mnt/media\u rw/udisk/sda4更好地访问你能试试这段代码吗,我希望它能工作
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();
}
}
导入java.io.File;
导入java.util.ArrayList;
导入java.util.Scanner;
导入android.os.Build;
导入android.os.Environment;
导入android.util.Log;
公共类存储选项{
私有静态ArrayList mMounts=新ArrayList();
私有静态ArrayList mVold=新ArrayList();
公共静态字符串[]标签;
公共静态字符串[]路径;
公共静态整数计数=0;
private static final String TAG=StorageOptions.class.getSimpleName();
公共静态void determineStorageOptions(){
readMountsFile();
readVoldFile();
compareMountsWithVold();
testAndCleanMountsList();
setProperties();
}
私有静态void readMountsFile(){
/*
*扫描/proc/mounts文件并查找以下行:
*/dev/block/vold/179:1/mnt/sdcard vfat
*rw、dirsync、nosuid、nodev、noexec、,
*relatime,uid=1000,gid=1015,fmask=0602,dmask
*=0602,允许使用时间=0020,代码页
*=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount ro 0
*
*找到一个后,将其拆分为元素,然后拉出
*指向该装入点的路径,并将其添加到arraylist
*/
//某些装载文件不列出默认值
//路径优先,因此我们将其添加到
//确保它是我们列表中的第一个
mMounts.add(“/mnt/sdcard”);
试一试{
Scanner Scanner=新扫描仪(新文件(“/proc/mounts”);
while(scanner.hasNext()){
字符串行=scanner.nextLine();
if(行.startsWith(“/dev/block/vold/”){
String[]lineElements=line.split(“”);
String元素=lineElements[1];
//不要添加默认装载路径
//它已经在列表中了。
如果(!element.equals(“/mnt/sdcard”))
mMounts.add(元素);
}
}
}捕获(例外e){
//自动生成的捕捉块
e、 printStackTrace();
}
for(字符串p:mMounts)
{
Log.i(“存储”,p);
}
}
私有静态void readVoldFile(){
/*
*扫描/system/etc/vold.fstab文件并查找以下行:
*开发安装SD卡/mnt/SD卡1
*/devices/platform/s3c-sdhci.0/mmc\u host/mmc0
*
*找到一个后,将其拆分为元素,然后拉出
*指向该装入点的路径,并将其添加到arraylist
*/
//某些设备完全缺少vold文件
//因此,我们在这里添加了一个路径,以确保列表始终
//包括第一个SD卡的路径,是否为实
//或者模仿。
mVold.add(“/mnt/sdcard”);
试一试{
Scanner Scanner=new Scanner(新文件(“/system/etc/vold.fstab”);
while(scanner.hasNext()){
字符串行=scanner.nextLine();
if(line.startsWith(“dev_mount”)){
String[]lineElements=line.split(“”);
String元素=lineElements[2];
if(element.contains(“:”)
element=element.substring(0,element.indexOf(“:”);
//不要添加默认卷路径
//它已经在列表中了。
如果(!element.equals(“/mnt/sdcard”))
mVold.add(元素);
}
}
}捕获(例外e){
//自动生成的捕捉块
e、 printStackTrace();
}
}
私有静态void compareMountsWithVold(){
/*
*有时两个挂载点列表会有所不同。我们只需要
*需要两个列表中的装载点。
*
*将这两个列表一起比较,并删除不在这两个列表中的项目
*名单。
*/
对于(int i=0;i0){
if(Build.VERSION.SDK_INT