package training.filesystem;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;import android.R.integer;import android.R.layout;import android.app.AlertDialog;import android.app.ListActivity;import android.content.DialogInterface;import android.os.Bundle;import android.view.Display;import android.view.Gravity;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.view.WindowManager;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.ArrayAdapter;import android.widget.LinearLayout.LayoutParams;import android.widget.Button;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.Spinner;import android.widget.TextView;import android.widget.Toast;public class FilesystemActivity extends ListActivity implements OnClickListener, OnItemSelectedListener {private TextView hint;private ListView list;private TextView filename;private Spinner filetype;private Button oKButton,cancelButton;private String root = "/";private String root_sdcard = "/mnt/sdcard";private ArrayList<String> path;private ArrayList<Map<String, Object>> item;private ArrayList<Version> aboutVersionList = new ArrayList<Version>();private String pathString;private String PATH_FILE_NAME;private AlertDialog warnAlertDialog;private AlertDialog errorAlertDialog;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.main);hint = (TextView) findViewById(R.id.path);filename =(TextView)findViewById(R.id.name);oKButton = (Button)findViewById(R.id.ok);cancelButton = (Button)findViewById(R.id.cancel);filetype = (Spinner)findViewById(R.id.spinner);filetype.setOnItemSelectedListener(this);list = this.getListView();oKButton.setOnClickListener(this);cancelButton.setOnClickListener(this);getfiledir(root_sdcard,0);WindowManager manager = getWindowManager();Display display = manager.getDefaultDisplay();android.view.WindowManager.LayoutParams params = getWindow().getAttributes();params.height = (int)(display.getHeight() * 0.5);params.width = (int)(display.getWidth() * 0.5);params.dimAmount = 0.0f;getWindow().setAttributes(params);getWindow().setGravity(Gravity.CENTER);}protected void getfiledir(String filepath,int type) {final int abcd = type;item = new ArrayList<Map<String, Object>>();path = new ArrayList<String>();hint.setText(filepath);pathString = filepath;File f = new File(filepath);File[] files = f.listFiles();if (!filepath.equals(root)) {Map<String, Object> map = new HashMap<String, Object>();map.put("icon", R.drawable.up_folder);map.put("name", "back to /");item.add(map);path.add("/mnt/sdcard/");Map<String, Object> map2 = new HashMap<String, Object>();map2.put("icon", R.drawable.up_folder);map2.put("name", "back to ../");item.add(map2);path.add(f.getParent());}if (files != null) {for (int i = 0; i < files.length; i++) {if(type == 0){Map<String, Object> map = new HashMap<String, Object>();if (files[i].isDirectory()) {map.put("icon", R.drawable.folder);}else {map.put("icon", R.drawable.file);}map.put("name", files[i].getName());item.add(map);path.add(files[i].getPath());}else if (type == 1) {if (files[i].isDirectory()) {Map<String , Object> map = new HashMap<String, Object>();map.put("icon", R.drawable.folder);map.put("name", files[i].getName());item.add(map);path.add(files[i].getPath());}}else if (type == 2) {if (files[i].isFile()) {Map<String, Object> map = new HashMap<String, Object>();map.put("icon", R.drawable.file);map.put("name", files[i].getName());item.add(map);path.add(files[i].getPath());}}}}SimpleAdapter simpleAdapter = new SimpleAdapter(FilesystemActivity.this, item, R.layout.line, new String[]{"icon","name"}, new int[]{R.id.icon,R.id.name});list.setAdapter(simpleAdapter);list.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {// TODO Auto-generated method stubFile file = new File(path.get(arg2));if (file.isDirectory()) {getfiledir(path.get(arg2),abcd);} else {Toast.makeText(FilesystemActivity.this, "这是一个文件!",Toast.LENGTH_SHORT).show();filename.setText(file.getName());PATH_FILE_NAME = file.getName();}}});}public boolean GetVersionInfo(){String tempString = null;boolean isHaveVersion = false;try {File file = new File(pathString + "/" + PATH_FILE_NAME);FileReader fileReader = new FileReader(file);BufferedReader bufferedReader = new BufferedReader(fileReader);while ((tempString = bufferedReader.readLine()) != null) {if (tempString.startsWith("[")) {if (tempString.substring(tempString.indexOf("[")+1,tempString.indexOf("]")).equals("Version")) {tempString = bufferedReader.readLine();Version version = new Version();String[] splitString = tempString.split(",");if (splitString.length != 3) {break;}version.VERSION_ID = splitString[0];version.VERSION_DEVICE = splitString[1];version.VERSION_PLATFORM = splitString[2];aboutVersionList.add(version);isHaveVersion = true;break;}}}} catch (Exception e) {// TODO: handle exceptione.printStackTrace();return false;}if (isHaveVersion) {return true;}else {return false;}}public class Version{String VERSION_ID;String VERSION_PLATFORM;String VERSION_DEVICE;}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif (v == oKButton) {if(GetVersionInfo()){Version version = aboutVersionList.get(0);warnAlertDialog = new AlertDialog.Builder(this).setIcon(R.drawable.warning).setTitle(getText(R.string.dialog_title)).setMessage(getText(R.string.dialog_content_id) + version.VERSION_ID +"\n" +getText(R.string.dialog_content_platform) + version.VERSION_PLATFORM + "\n" +getText(R.string.dialog_content_device) + version.VERSION_DEVICE + "\n" +getText(R.string.dialog_content)).setPositiveButton(getText(R.string.dialog_ok), new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}}).setNegativeButton(getText(R.string.dialog_cancel), new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}}).show();}else {errorAlertDialog = new AlertDialog.Builder(this).setIcon(R.drawable.warning).setTitle(getText(R.string.dialog_title)).setMessage(getText(R.string.dialog_error)).setPositiveButton(getText(R.string.dialog_ok), new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}}).show();}}}@Overridepublic void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {// TODO Auto-generated method stubString aString = filetype.getSelectedItem().toString();if (aString.equals("所有档案")) {getfiledir(pathString,0);}else if (aString.equals("文件夹")) {getfiledir(pathString, 1);}else if (aString.equals("文件")) {getfiledir(pathString, 2);}}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub}}

更多相关文章

  1. Android(安卓)DEX方法超过64K和gradle编译OOM解决方案
  2. android 记得用mkdirs不要用mkdir
  3. android 获取图片绝对地址
  4. Android运行虚拟机报错Execution failed for task ':app:transfo
  5. Android操作SD卡文件
  6. android一个简单的线程实例
  7. Android之针对webview的缓存
  8. Android中对话框的使用
  9. NPM 和webpack 的基础使用

随机推荐

  1. Android之判断当前指定App是否在前台
  2. ProgressBar进度条
  3. minSdkVersion、targetSdkVersion、targe
  4. Android 5.0 API 的变化——开发人员注意
  5. Android编译报错
  6. Android电话拨号器实现方法
  7. 在非主线程中不能操作主线程中的View
  8. Android 泛型使用
  9. Android语音识别功能使用总结
  10. Android 弹出菜单