2017-01-18 20 views
0

これらの回答のいずれかを使用してすべてのストレージを取得する方法を既に知っています:with proc/mountswith mount commandですが、今度は返されたパスに名前を付けたいと思います。内部ストレージには問題ありませんが、これらの方法ではSDカードとUSBスティックを区別できません。 2台のドライブが接続されている場合は、どちらがSDカードで、どれがUSBであるかを確認するにはどうすればいいですか?または2つのUSBドライブ?すべてのストレージとデバイス名を取得するandroid

答えて

1

私は解決策の一部を見つけましたが、複雑になっていて、これは私のアプリの主な機能ではありません。

public static class Storage extends File { 

    public static final int INTERNAL_STORAGE = 1; 
    public static final int SD_CARD = 2; 
    public static final int USB_DRIVE = 3; 

    public String name; 
    public int type; 

    public Storage(String path, String name, int type) { 
     super(path); 
     this.name = name; 
     this.type = type; 
    } 
} 

public static ArrayList<Storage> getStorages(Context context) { 
    ArrayList<Storage> storages = new ArrayList<>(); 

    // Internal storage 
    storages.add(new Storage(Environment.getExternalStorageDirectory().getPath(), 
      "Internal Storage", Storage.INTERNAL_STORAGE)); 

    // SD Cards 
    ArrayList<File> extStorages = new ArrayList<>(); 
    extStorages.addAll(Arrays.asList(context.getExternalFilesDirs(null))); 
    extStorages.remove(0); // Remove internal storage 
    String secondaryStoragePath = System.getenv("SECONDARY_STORAGE"); 
    for (int i = 0; i < extStorages.size(); i++) { 
     String path = extStorages.get(i).getPath().split("/Android")[0]; 
     if (Environment.isExternalStorageRemovable(extStorages.get(i)) || secondaryStoragePath != null && secondaryStoragePath.contains(path)) { 
      String name = "SD Card" + (i == 0 ? "" : " " + String.valueOf(i+1)); 
      storages.add(new Storage(path, name, Storage.SD_CARD)); 
     } 
    } 

    // USB Drives 
    ArrayList<String> drives = new ArrayList<>(); 
    String reg = "(?i).*vold.*(vfat|ntfs|exfat|fat32|ext3|ext4).*rw.*"; 
    String s = ""; 
    try { 
     final Process process = new ProcessBuilder().command("mount") 
       .redirectErrorStream(true).start(); 
     process.waitFor(); 
     final InputStream is = process.getInputStream(); 
     final byte[] buffer = new byte[1024]; 
     while (is.read(buffer) != -1) { 
      s += new String(buffer); 
     } 
     is.close(); 
    } catch (final Exception e) { 
     e.printStackTrace(); 
    } 
    final String[] lines = s.split("\n"); 
    for (String line : lines) { 
     if (!line.toLowerCase(Locale.US).contains("asec") && line.matches(reg)) { 
      String[] parts = line.split(" "); 
      for (String path : parts) { 
       if (path.startsWith(File.separator) && !path.toLowerCase(Locale.US).contains("vold")) { 
        drives.add(path); 
       } 
      } 
     } 
    } 

    // Remove SD Cards from found drives (already found) 
    ArrayList<String> ids = new ArrayList<>(); 
    for (Storage st : storages) { 
     String[] parts = st.getPath().split(File.separator); 
     ids.add(parts[parts.length-1]); 
    } 
    for (int i = drives.size() - 1; i >= 0; i--) { 
     String[] parts = drives.get(i).split(File.separator); 
     String id = parts[parts.length-1]; 
     if (ids.contains(id)) drives.remove(i); 
    } 

    // Get USB Drive name 
    UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE); 
    Collection<UsbDevice> dList = usbManager.getDeviceList().values(); 
    ArrayList<UsbDevice> deviceList = new ArrayList<>(); 
    deviceList.addAll(dList); 
    for (int i = 0; i < deviceList.size(); i++) { 
     storages.add(new Storage(drives.get(i), deviceList.get(i).getProductName(), Storage.USB_DRIVE)); 
    } 

    return storages; 
} 
  • 内部ストレージ
  • は、彼らがすでに発見されたので、彼らからSDカードを取り外した後、すべての外部ドライブを検索
  • すべてのSDカードを探します。これは、SDカードとUSBデバイスを分離することです。

私はこれ以外のデバイスでこれをテストしませんでしたが、すべてのデバイスでうまく機能するとは限りません。また、キーボードやマウスが接続されたデバイスとしてカウントされる可能性もあります。

+0

である。 – cagdas

関連する問題