How to get path to Storage directory in android

江枫思渺然 提交于 2020-01-11 04:17:09

问题


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.


回答1:


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.

For that you can use this,

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;
    }

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.



来源:https://stackoverflow.com/questions/32064199/how-to-get-path-to-storage-directory-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!