Guest User

Untitled

a guest
Jul 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. public void getUSBHostName(Context context) {
  2. StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
  3. try {
  4. Method getVolumeList = StorageManager.class.getDeclaredMethod("getVolumeList", null);
  5. getVolumeList.setAccessible(true);
  6.  
  7. Class<?> classStorageVolume = Class.forName("android.os.storage.StorageVolume");
  8. Method getPath = classStorageVolume.getDeclaredMethod("getPath", null);
  9. Method getLabel = classStorageVolume.getDeclaredMethod("getUserLabel", null);
  10. Object[] volumes = (Object[]) getVolumeList.invoke(storageManager, null);
  11. Log.i(TAG, "detectUSBName: +++" + volumes);
  12. if (volumes != null) {
  13. for (int i = 0; i < volumes.length; i++) {
  14. String path = (String) getPath.invoke(volumes[i], null);
  15. String label = (String) getLabel.invoke(volumes[i], null);
  16. Log.i(TAG, "detectUSBName Path:" + path + " Label:" + label);
  17. }
  18. }
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. }
Add Comment
Please, Sign In to add comment