zoukankan      html  css  js  c++  java
  • 结对项目——本地电子书的导入

    结对项目中对本地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());
    		
    	}
    

    界面

  • 相关阅读:
    14.3.2.1 Transaction Isolation Levels 事务隔离级别
    ReentrantLock可重入锁
    Lock与synchronized 的区别
    synchronized 与 Lock
    This usually indicates a missing no-arg constructor or that the editor's class name was mistyped in
    Oracle dump 分析secondary key
    java.lang.ClassNotFoundException: org.springframework.web.util.IntrospectorCleanupListener
    Oracle 验证IOT表数据存储在主键里
    Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for U
    Oracle heap 表的主键 dump 分析
  • 原文地址:https://www.cnblogs.com/lw0607/p/6991800.html
Copyright © 2011-2022 走看看