-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathUsbUtils2.java
More file actions
94 lines (80 loc) · 2.46 KB
/
UsbUtils2.java
File metadata and controls
94 lines (80 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.newline.initial.setting.utils;
import android.content.Context;
import android.os.storage.StorageManager;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
/**
* @author Realmo
* @version 1.0.0
* @name NewlineInitialSetting
* @email momo.weiye@gmail.com
* @time 2020/4/10 15:23
* @describe
*/
public class UsbUtils {
public static Object[] getStoragePaths(Context context) {
StorageManager storageManager = (StorageManager) context
.getSystemService(Context.STORAGE_SERVICE);
try {
Class<?>[] paramClasses = {};
Method getVolumeList = StorageManager.class.getMethod("getVolumeList", paramClasses);
Object[] params = {};
Object[] invokes = (Object[]) getVolumeList.invoke(storageManager, params);
if (invokes != null) {
return invokes;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
*
* @param obj 实为StorageVolume对象
* @return usb路径
*/
public static String getStoragePath(Object obj){
try {
Method getPath = obj.getClass().getMethod("getPath", new Class[0]);
return (String) getPath.invoke(obj, new Object[0]);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
/**
* 是否为自身的存储设备
* @param path
* @return
*/
public static boolean isSelfStorage(String path){
if("/storage/emulated/0".equals(path)){
return true;
}
return false;
}
/**
*
* @param obj 实为StorageVolume对象
* @return usb标签名
*/
public static String getUsbDeviceLabel(Object obj){
try {
Method getUserLabel = obj.getClass().getMethod("getUserLabel",new Class[0]);
return (String) getUserLabel.invoke(obj,new Object[0]);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return "Unknown";
}
}