-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathUsbUtils.java
More file actions
143 lines (121 loc) · 3.51 KB
/
Copy pathUsbUtils.java
File metadata and controls
143 lines (121 loc) · 3.51 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.realmo.utils;
import android.content.Context;
import android.os.Environment;
import android.os.storage.StorageManager;
import android.util.Log;
import com.mstar.android.storage.MStorageManager;
import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
/**
* @author RealMo
* @date ´´½¨Ê±¼ä£º2017-8-31 ÉÏÎç9:09:45
* @version 1.0
* @parameter
* @since
* @return
*/
public class UsbUtils {
public static final String SAME_FILE="same_file";
/**
*
* @param context
* @return usb root path array
*/
public static String[] getStoragePaths(Context context) {
List<String> pathsList = new ArrayList<String>();
StorageManager storageManager = (StorageManager) context
.getSystemService(Context.STORAGE_SERVICE);
try {
Method method = StorageManager.class
.getDeclaredMethod("getVolumePaths");
method.setAccessible(true);
Object result = method.invoke(storageManager);
if (result != null && result instanceof String[]) {
String[] pathes = (String[]) result;
for (String path : pathes) {
if (path.contains("/sdcard") || path.contains("/storage"))
continue;
pathsList.add(path);
}
}
} catch (Exception e) {
e.printStackTrace();
File externalFolder = Environment.getExternalStorageDirectory();
if (externalFolder != null) {
pathsList.add(externalFolder.getAbsolutePath());
}
}
for (String string : pathsList) {
Log.d("realmo", "usb path-->:" + string);
}
return pathsList.toArray(new String[pathsList.size()]);
}
/**
*
* @param
* @return usb devices label/name
*/
public static String getUsbDeviceLabel(Context context, String strMountPath) {
String strLabel = "USB";
StorageManager mStorageManager = (StorageManager) context
.getSystemService(Context.STORAGE_SERVICE);
MStorageManager sm = MStorageManager.getInstance(context);
if (Environment.MEDIA_MOUNTED.equals(sm.getVolumeState(strMountPath))) {
strLabel = sm.getVolumeLabel(strMountPath);
}
if ((strLabel == null) || strLabel.isEmpty()) {
strLabel = "USB";
}
Log.d("realmo", "strMountPath-->" + strMountPath);
Log.d("realmo", "strLabel-->" + strLabel);
return strLabel;
}
/**
*
* @param childPath
* like:x9/x9.png
* @param usbRootPathArray
* like:/mnt/usb/sdc1
* @param selectUsbType
* @return filePath:usbRootPath+childPath
*/
public static String getSelectUsbDeviceFilePath(String childPath,
String[] usbRootPathArray, String selectUsbType) {
int usbDevicesCount = usbRootPathArray.length;
// only one usb device situation
if (usbDevicesCount == 1) {
return usbRootPathArray[0] + "/" + childPath;
}
// more usb devices situation
if (usbDevicesCount > 1) {
List<File> exsitFiles = new ArrayList<File>();
File[] files = new File[usbDevicesCount];
for (int i = 0; i < usbDevicesCount; i++) {
files[i] = new File(usbRootPathArray[i] + "/" + childPath);
if (files[i].exists()) {
exsitFiles.add(files[i]);
}
}
if (exsitFiles.size() == 1) {
return exsitFiles.get(0).getAbsolutePath();
}
// Determine whether there is the same file
else if (exsitFiles.size() > 1) {
// Determine usbType realmo:no way
// for (int i = 0; i < exsitFiles.size(); i++) {
// if (Uri.fromFile(exsitFiles.get(i))
// .getEncodedPath().contains(selectUsbType)) {
// return exsitFiles.get(i).getAbsolutePath();
//
// }
//
// }
// return constant flag
return SAME_FILE;
}
}
return null;
}
}