Browse Source

listFiles插件代码提交

zhanglong7 4 years ago
parent
commit
653307ae6e

+ 41 - 1
ipu-plugin-basic/src/main/java/com/ai/ipu/mobile/plugin/MobileFile.java

4

4

5
import org.json.JSONArray;
5
import org.json.JSONArray;
6
import org.json.JSONException;
6
import org.json.JSONException;
7
import org.json.JSONObject;
7

8

8
import android.content.ActivityNotFoundException;
9
import android.content.ActivityNotFoundException;
9
import android.widget.Toast;
10
import android.widget.Toast;
102
		}
103
		}
103
		return arr;
104
		return arr;
104
	}
105
	}
105
	
106

107
	/**
108
	 * 列出指定目录下的文件
109
	 * @param param 目录
110
	 * @throws Exception
111
	 */
112
	public void listFiles(JSONArray param) throws Exception {
113
		final String keyType = "type";
114
		final String keyName = "name";
115
		String path = param.getString(0);
116
		String absolutePath = DirectionUtil.getInstance(this.context).getDirection(path, true);
117
		File dir = new File(absolutePath);
118
		if (!dir.exists()) {
119
			this.error("请输入正确文件夹地址");
120
		} else if (dir.isFile()) {
121
			this.error("请输入正确文件夹");
122
		} else {
123
			File[] files = dir.listFiles();
124
			JSONArray arr = new JSONArray();
125

126
			if (files != null && files.length > 0) {
127
				int length = files.length;
128

129
				for(int i = 0; i < length; ++i) {
130
					File item = files[i];
131
					JSONObject jsonItem = new JSONObject();
132
					jsonItem.put(keyName, item.getName());
133
					if (item.isFile()) {
134
						jsonItem.put(keyType, "F");
135
					} else if (item.isDirectory()) {
136
						jsonItem.put(keyType, "D");
137
					}
138

139
					arr.put(jsonItem);
140
				}
141
			}
142

143
			this.callback(arr.toString());
144
		}
145
	}
106
	/**
146
	/**
107
	 * 打开指定文件
147
	 * 打开指定文件
108
	 */
148
	 */