• 结对项目——本地电子书的导入


    结对项目中对本地SD卡电子书的导入
    从SD卡中获取文件并判断文件的类型从而实现对电子书的导入

    private BookManager bookManager;
    	private File rootDir;
    	private EditText filePath;
    	private XListView mListView;
    	private Handler mHandler;
    	
    	
       
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.import_book);
    		initView();
    	}
    
    	private void initView() {
    		 mHandler = new Handler();
    		bookManager = new BookManager(this);
    		mListView = (XListView) findViewById(R.id.listView);
    		mListView.setPullRefreshEnable(true);
            mListView.setPullLoadEnable(true);
            mListView.setAutoLoadEnable(true);
            mListView.setXListViewListener(this);
            mListView.setRefreshTime(getTime());
    		filePath = (EditText) findViewById(R.id.filePath);
    		// SDCard作为根目录
    		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
    			rootDir = Environment.getExternalStorageDirectory();
    		} else {
    			rootDir = Environment.getRootDirectory();
    		}// mnt/sdcard
    		loadData(rootDir);
    		mListView.setOnItemClickListener(new OnItemClickListener() {
    			
    			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    				HashMap map = (HashMap) parent.getItemAtPosition(position);
    				File selectFile = (File) map.get("file");
    				if (selectFile.isDirectory()) {// 是文件夹
    					loadData(selectFile);
    				} else if (selectFile.exists()) {
    					if (selectFile.length() < 1) {
    						ToastUtils.toast(ImportBook.this, "此文件是无效文件");
    						return;
    					}
    					Book book = new Book(selectFile.getName(), selectFile.getAbsolutePath(), new Date(), 0, "0.00%");
    					bookManager.save(book);
    					Intent intent = new Intent();
    					ImportBook.this.setResult(RESULT_OK, intent);
    					ImportBook.this.finish();
    				} else {
    					ToastUtils.toast(ImportBook.this, "文件不存在");
    				}
    			}
    		});
    		
    	}
    
    	// 加载数据
    	private void loadData(File file) {
    		File[] files = FileUtils.read(file);
    		Log.d(TAG, "files.length:" + files.length);
    		ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
    
    		if (!rootDir.getAbsolutePath().equals(file.getAbsolutePath())) {
    			HashMap<String, Object> map = new HashMap<String, Object>();
    			map.put("image", R.drawable.file);
    			map.put("file", file.getParentFile());
    			map.put("name", "返回上一级...");
    			data.add(map);
    		}
    
    		for (int i = 0; i < files.length; i++) {
    			HashMap<String, Object> map = new HashMap<String, Object>();
    			String fileName = files[i].getName();
    
    			if (files[i].isDirectory()) {// 文件
    				map.put("image", R.drawable.file);
    			} else if (files[i].isFile()) {
    				if (fileName.endsWith(".txt")) {
    					map.put("image", R.drawable.txt);
    				} else if (fileName.endsWith(".zip")) {
    					map.put("image", R.drawable.zip);
    				} else if (fileName.endsWith(".xml")) {
    					map.put("image", R.drawable.xml);
    				}
    			}
    			map.put("file", files[i]);
    			map.put("name", fileName);
    			if (files[i].listFiles() != null) {
    				map.put("size", "(" + files[i].listFiles().length + ")");
    			} else if (files[i].isFile()) {
    			} else {
    				map.put("size", "(0)");
    			}
    			map.put("time", DateUtils.format(files[i].lastModified()));
    			data.add(map);
    		}
    
    		SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.import_book_item, new String[] { "image", "name", "size", "time" }, new int[] { R.id.image, R.id.name,
    				R.id.size, R.id.time });
    		mListView.setAdapter(adapter);
    		filePath.setText("路径:" + file.getAbsolutePath());
    		Log.d(TAG, "data.size():" + data.size());
    		
    	}
    

    界面

  • 相关阅读:
    查看oracle死锁进程并结束死锁
    用windows 定时任务执行kettle的ktr文件,以及问题处理
    Sentinel 高可用流量管理框架
    python
    Excel Vlookup用法和常见报错#REF! #Value!
    OutLook从excel导入联系人
    配置windows server多个用户同时使用一个账户远程服务器
    从Sql Server转战Oracle 之 变量声明
    数据库tempdb的事物日志已满,原因为“ACTIVE_TRANSACTION”
    Chapter 2
  • 原文地址:https://www.cnblogs.com/lw0607/p/6991800.html
Copyright © 2020-2023  润新知