Android File System Volume Get getVolumeName(final String devicePath)

Here you can find the source of getVolumeName(final String devicePath)

Description

Gets the volume name.

License

Open Source License

Parameter

Parameter Description
devicePath the path of the the device e.g. <code>C:</code>

Return

the volume name associated with the device or null if no name is associated with the device

Declaration

public static String getVolumeName(final String devicePath) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import javax.swing.filechooser.FileSystemView;

public class Main {
    /**//from w w w  .j  a  v a  2s.  c om
     * Gets the volume name.
     * 
     * @param devicePath the path of the the device e.g. <code>C:</code>
     * @return the volume name associated with the device or null if no name is associated
     *         with the device
     */
    public static String getVolumeName(final String devicePath) {
        if (devicePath == null) {
            throw new IllegalArgumentException(
                    "The volume name should be assigned");
        }
        final FileSystemView view = FileSystemView.getFileSystemView();
        final File dir = new File(devicePath);
        final String name = view.getSystemDisplayName(dir);
        if (name == null) {
            return null;
        }
        final String trimedName = name.trim();
        if (trimedName.length() < 1) {
            return null;
        }

        /*
         * take the clean volume name by striping from the format: Volname (E:)
         */
        final int index = trimedName.lastIndexOf(" (");

        final String cleanName;
        if (index > 0) {
            cleanName = trimedName.substring(0, index);
        } else {
            cleanName = trimedName;
        }
        return cleanName;
    }
}