简体   繁体   English

如何获得 SD 卡 label?

[英]How can I get SD Card label?

I'm developing an app where I can delete files from the SD Card, and I also want to display the SD Card label, NOT the SD Card path, as there is a lot of informations on this already, but not that much about External Storage name.我正在开发一个应用程序,我可以在其中删除 SD 卡中的文件,并且我还想显示 SD 卡 label,而不是 SD 卡路径,因为已经有很多信息,但关于外部的信息不多存储名称。 When I'm talking about SD Card name, I mean the thing that is showed up when you mount it on a PC and in your file explorer it shows you the name of it like "John D SD Card"当我谈论 SD 卡名称时,我的意思是当您将其安装在 PC 上并在文件资源管理器中显示它的名称时,它会显示它的名称,例如“John D SD 卡”

Any help about that would be appreciated !对此的任何帮助将不胜感激! Thanks谢谢

For Android 7/N and up:对于 Android 7/N 及以上:

StorageManager sm = (StorageManager)getSystemService(Context.STORAGE_SERVICE);

List<StorageVolume> volumes = sm.getStorageVolumes();

StringBuilder sb = new StringBuilder();

int nr = -1;
while (++nr < volumes.size() )
    {
    StorageVolume volume = volumes.get(nr);
    
    sb.append(volume.getUuid()).append("\n");
    sb.append(volume.getState()).append("\n");
    //sb.append(volume.EXTRA_STORAGE_VOLUME + "\n");
    sb.append(volume.getDescription(context)).append("\n");
    sb.append(volume.toString()).append("\n");
    if ( VERSION.SDK_INT >= Build.VERSION_CODES.R)
            sb.append(volume.getDirectory()).append("\n");
    sb.append( "\n");
    }

        
Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM