如何在Android中获取存储目录的路径 [英] How to get path to Storage directory in android

查看:991
本文介绍了如何在Android中获取存储目录的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何获取/storage/目录的方法.我尝试了Environment.getExternalStorageDirectory(),但是它返回了/storage/emulated/0,我知道我可以使用file.getParent(),但是由于某些原因,我不能使用它.我只想使用某些功能到/storage/目录的直线路径...在此先感谢.

Is there any method to get /storage/ directory. I tried with Environment.getExternalStorageDirectory() but it returns /storage/emulated/0 i know i can use file.getParent() but for some reason i cant use that. I just want a straight path to /storage/ directory using some function... Thanks in advance.

推荐答案

您不能也不应正常访问该目录.但似乎您需要对设备可用的存储位置进行更多控制.

You can't and should not access that directory normally. But it seems you need more control on storage locations are available to your device.

为此,您可以使用它,

public List<String> getStorageDirectories() {

        // Final set of paths
        final ArrayList<String> finalPaths = new ArrayList<String>();

        // Must add the ROOT directory
        finalPaths.add("/");

        // Primary physical SD-CARD (not emulated)
        final String rawExternalStorage = System.getenv("EXTERNAL_STORAGE");

        // All Secondary SD-CARDs (all exclude primary) separated by ":"
        final String rawSecondaryStoragesStr = System.getenv("SECONDARY_STORAGE");

        // Primary emulated SD-CARD
        final String rawEmulatedStorageTarget = System.getenv("EMULATED_STORAGE_TARGET");

        if (TextUtils.isEmpty(rawEmulatedStorageTarget)) {

            // Device has physical external storage; use plain paths.
            if (TextUtils.isEmpty(rawExternalStorage)) {

                // EXTERNAL_STORAGE undefined; falling back to default.
                finalPaths.add("/storage/sdcard0");
            } else {
                finalPaths.add(rawExternalStorage);
            }
        } else {

            // Device has emulated storage; external storage paths should have
            // userId burned into them.
            final String rawUserId;
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
                rawUserId = "";
            } else {

                final String path = Environment.getExternalStorageDirectory().getAbsolutePath();
                final String[] folders = DIR_SEPARATOR.split(path);
                final String lastFolder = folders[folders.length - 1];
                boolean isDigit = false;
                try {
                    Integer.valueOf(lastFolder);
                    isDigit = true;
                } catch (NumberFormatException ignored) {
                }
                rawUserId = isDigit ? lastFolder : "";
            }
            // /storage/emulated/0[1,2,...]
            if (TextUtils.isEmpty(rawUserId)) {
                finalPaths.add(rawEmulatedStorageTarget);
            } else {
                finalPaths.add(rawEmulatedStorageTarget + File.separator + rawUserId);
            }
        }
        // Add all secondary storages
        if (!TextUtils.isEmpty(rawSecondaryStoragesStr)) {
            // All Secondary SD-CARDs splited into array
            final String[] rawSecondaryStorages = rawSecondaryStoragesStr.split(File.pathSeparator);
            Collections.addAll(finalPaths, rawSecondaryStorages);
        }

        File usb = getUsbDrive();

        if (usb != null && !finalPaths.contains(usb.getPath()))
            finalPaths.add(usb.getPath());

        return finalPaths;
    }

这是将USB驱动器连接到设备的方法,

And here is the method to get the USB drive attached to your device,

public File getUsbDrive() {
        File parent;
        parent = new File("/storage");

        try {
            for (File f : parent.listFiles()) {
                if (f.exists() && f.getName().toLowerCase().contains("usb") && f.canExecute()) {
                    return f;
                }
            }
        } catch (Exception e) {
        }
        parent = new File("/mnt/sdcard/usbStorage");
        if (parent.exists() && parent.canExecute())
            return (parent);
        parent = new File("/mnt/sdcard/usb_storage");
        if (parent.exists() && parent.canExecute())
            return parent;

        return null;
    }

但是请记住,这不是官方方法,而是黑客入侵,因此使用此方法后果自负.

But remember that this is not an official way but a hack, so use it at your own risk.

这篇关于如何在Android中获取存储目录的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆