Building an Android FileBrowser (list-based)
来源:互联网 发布:乐视手机mac怎么修改 编辑:程序博客网 时间:2024/06/02 00:14
The Full Source:
res/layout/file_row.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <TextView
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <?xmlversion="1.0"encoding="utf-8"?>
- <resources>
- <stringname="app_name">Android File Browser - by anddev.org</string>
- <stringname="up_one_level">..</string>
- <stringname="current_dir">.</string>
- </resources>
- package org.anddev.android.filebrowser;
- import java.io.File;
- import java.net.URISyntaxException;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.AlertDialog;
- import android.app.ListActivity;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.DialogInterface.OnClickListener;
- import android.net.ContentURI;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- public class AndroidFileBrowser extends ListActivity{
- private enum DISPLAYMODE{ ABSOLUTE, RELATIVE;}
- private final DISPLAYMODE displayMode = DISPLAYMODE.ABSOLUTE;
- private List<String> directoryEntries= new ArrayList<String>();
- private File currentDirectory = new File("/");
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle icicle){
- super.onCreate(icicle);
- // setContentView() gets called within the next line,
- // so we do not need it here.
- browseToRoot();
- }
- /**
- * This function browses to the
- * root-directory of the file-system.
- */
- private void browseToRoot(){
- browseTo(newFile("/"));
- }
- /**
- * This function browses up one level
- * according to the field: currentDirectory
- */
- private void upOneLevel(){
- if(this.currentDirectory.getParent()!= null)
- this.browseTo(this.currentDirectory.getParentFile());
- }
- private void browseTo(finalFile aDirectory){
- if (aDirectory.isDirectory()){
- this.currentDirectory= aDirectory;
- fill(aDirectory.listFiles());
- }else{
- OnClickListener okButtonListener = new OnClickListener(){
- // @Override
- publicvoid onClick(DialogInterface arg0,int arg1){
- try {
- // Lets start an intent to View the file, that was clicked...
- Intent myIntent = new Intent(android.content.Intent.VIEW_ACTION,
- new ContentURI("file://"
- + aDirectory.getAbsolutePath()));
- startActivity(myIntent);
- } catch (URISyntaxException e){
- e.printStackTrace();
- }
- }
- };
- OnClickListener cancelButtonListener = new OnClickListener(){
- // @Override
- publicvoid onClick(DialogInterface arg0,int arg1){
- // Do nothing
- }
- };
- AlertDialog.show(this,"Question","Do you want to open that file?n"
- + aDirectory.getName(),
- "OK", okButtonListener,
- "Cancel", cancelButtonListener, false, null);
- }
- }
- private void fill(File[] files){
- this.directoryEntries.clear();
- // Add the "." and the ".." == 'Up one level'
- try {
- Thread.sleep(10);
- } catch (InterruptedException e1){
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- this.directoryEntries.add(".");
- if(this.currentDirectory.getParent()!= null)
- this.directoryEntries.add("..");
- switch(this.displayMode){
- case ABSOLUTE:
- for(File file : files){
- this.directoryEntries.add(file.getPath());
- }
- break;
- case RELATIVE:// On relative Mode, we have to add the current-path to the beginning
- int currentPathStringLenght= this.currentDirectory.getAbsolutePath().length();
- for(File file : files){
- this.directoryEntries.add(file.getAbsolutePath().substring(currentPathStringLenght));
- }
- break;
- }
- ArrayAdapter<String> directoryList= new ArrayAdapter<String>(this,
- R.layout.file_row,this.directoryEntries);
- this.setListAdapter(directoryList);
- }
- @Override
- protected void onListItemClick(ListView l,View v, int position, long id){
- int selectionRowID= (int)this.getSelectionRowID();
- String selectedFileString = this.directoryEntries.get(selectionRowID);
- if (selectedFileString.equals(".")){
- // Refresh
- this.browseTo(this.currentDirectory);
- } else if(selectedFileString.equals("..")){
- this.upOneLevel();
- } else {
- File clickedFile = null;
- switch(this.displayMode){
- case RELATIVE:
- clickedFile =new File(this.currentDirectory.getAbsolutePath()
- + this.directoryEntries.get(selectionRowID));
- break;
- case ABSOLUTE:
- clickedFile =new File(this.directoryEntries.get(selectionRowID));
- break;
- }
- if(clickedFile!= null)
- this.browseTo(clickedFile);
- }
- }
- }
- Building an Android FileBrowser (list-based)
- Building an AAR Library in Android Studio
- Building customer models from business data: an automatic approach based on fuzzy clustering maching
- data structure: static linked list / hash linking / an array based linked list
- A first hand look at building an Android application
- Training ------ Building Your First App ------ Creating An Android Project
- android-Building a Notification,Preserving Navigation when Starting an Activity
- Building an Agile Team
- Unity3D-FileBrowser
- Building Hadoop-based Apps on YARN
- Building your own kernel based on CentOS
- CSS Page Layout: Building Float-Based Layouts
- Building an ASP.NET Intranet
- Building an Ajax Memory Tree
- Building an OpenGL ES Environment
- building Android
- How to Build Android Applications Based on FFmpeg by An Example
- How to Build Android Applications Based on FFmpeg by An Example
- STL中的for_each调用类成员函数,mem_fun, mem_fun_ref用法
- php 温故
- 第一版Python程序
- Datatable 增加一列并为该列赋上相应的值
- LIS (nlogn解法)
- Building an Android FileBrowser (list-based)
- 论“日新首相再次鼓吹中国威胁说会阻止中国南下”
- 根文件系统与文件系统的区别
- 继电器RC电路问题
- 将Java doc转换为chm的方法
- hdu_2031_进制转换_oj也只是机器...
- ubuntun下cppcms安装
- 用DOS批处理实现FTP自动上传、下载、清理文件
- SandBox中方便、精确地选择物品