/*
* [程序名称] Android 文件浏览器
* [作者] xmobileapp团队
* [参考资料] www.anddev.org
* [开源协议] Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.xmobileapp.filebrowser;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.xmobileapp.filebrowser.iconifiedlist.IconifiedText;
import com.xmobileapp.filebrowser.iconifiedlist.IconifiedTextListAdapter;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
public class FileBrowser extends ListActivity {
private enum DISPLAYMODE{ ABSOLUTE, RELATIVE; }
protected static final int SUB_ACTIVITY_REQUEST_CODE = 1337;
private final DISPLAYMODE displayMode = DISPLAYMODE.RELATIVE;
private List<IconifiedText> directoryEntries = new ArrayList<IconifiedText>();
private File currentDirectory = new File("/");
/** Activity被创建时调用 */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setTheme(android.R.style.Theme_Black);
browseToRoot();
this.setSelection(0);
}
/**
* This function browses to the
* root-directory of the file-system.
*/
private void browseToRoot() {
browseTo(new File("/"));
}
/**
* 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(final File aDirectory){
// On relative we display the full path in the title.
if(this.displayMode == DISPLAYMODE.RELATIVE)
this.setTitle(aDirectory.getAbsolutePath() + " :: " +
getString(R.string.app_name));
if (aDirectory.isDirectory()){
this.currentDirectory = aDirectory;
fill(aDirectory.listFiles());
}else{
// Show an Alert with the ButtonListeners we created
new AlertDialog.Builder(this).setTitle("提示")
.setMessage("本例不支持文件打开操作!")
.create().show();
}
}
private void fill(File[] files) {
this.directoryEntries.clear();
// Add the "." == "current directory"
this.directoryEntries.add(new IconifiedText(
getString(R.string.current_dir),
getResources().getDrawable(R.drawable.folder)));
// and the ".." == 'Up one level'
if(this.currentDirectory.getParent() != null)
this.directoryEntries.add(new IconifiedText(
getString(R.string.up_one_level),
getResources().getDrawable(R.drawable.uponelevel)));
Drawable currentIcon = null;
for (File currentFile : files){
if (currentFile.isDirectory()) {
currentIcon = getResources().getDrawable(R.drawable.folder);
}else{
String fileName = currentFile.getName();
/* Determine the Icon to be used,
* depending on the FileEndings defined in:
* res/values/fileendings.xml. */
if(checkEndsWithInStringArray(fileName, getResources().
getStringArray(R.array.fileEndingImage))){
currentIcon = getResources().getDrawable(R.drawable.image);
}else if(checkEndsWithInStringArray(fileName, getResources().
getStringArray(R.array.fileEndingWebText))){
currentIcon = getResources().getDrawable(R.drawable.webtext);
}else if(checkEndsWithInStringArray(fileName, getResources().
getStringArray(R.array.fileEndingPackage))){
currentIcon = getResources().getDrawable(R.drawable.packed);
}else if(checkEndsWithInStringArray(fileName, getResources().
getStringArray(R.array.fileEndingAudio))){
currentIcon = getResources().getDrawable(R.drawable.audio);
}else{
currentIcon = getResources().getDrawable(R.drawable.text);
}
}
switch (this.displayMode) {
case ABSOLUTE:
/* On absolute Mode, we show the full path */
this.directoryEntries.add(new IconifiedText(currentFile
.getPath(), currentIcon));
break;
case RELATIVE:
/* On relative Mode, we have to cut the
* current-path at the beginning */
int currentPathStringLenght = this.currentDirectory.
getAbsolutePath().length();
this.directoryEntries.add(new IconifiedText(
currentFile.getAbsolutePath().
substring(currentPathStringLenght),
currentIcon));
break;
}
}
Collections.sort(this.directoryEntries);
IconifiedTextListAdapter itla = new IconifiedTextListAdapter(this);
itla.setListItems(this.directoryEntries);
this.setListAdapter(itla);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
int selectionRowID = (int) this.getSelectedItemId();
String selectedFileString = this.directoryEntries.get(selectionRowID).getText();
if (selectedFileString.equals(getString(R.string.current_dir))) {
// Refresh
this.browseTo(this.currentDirectory);
} else if(selectedFileString.equals(getString(R.string.up_one_level))){
this.upOneLevel();
} else {
File clickedFile = null;
switch(this.displayMode){
case RELATIVE:
clickedFile = new File(this.currentDirectory.getAbsolutePath()
+ this.directoryEntries.get(selectionRowID).getText());
break;
case ABSOLUTE:
clickedFile = new File(this.directoryEntries.get(selectionRowID).getText());
break;
}
if(clickedFile != null)
this.browseTo(clickedFile);
}
}
/** Checks whether checkItsEnd ends with
* one of the Strings from fileEndings */
private boolean checkEndsWithInStringArray(String checkItsEnd,
String[] fileEndings){
for(String aEnd : fileEndings){
if(checkItsEnd.endsWith(aEnd))
return true;
}
return false;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
安卓开发实例 文件浏览器
共33个文件
class:10个
png:9个
java:5个
5星 · 超过95%的资源 需积分: 11 33 下载量 102 浏览量
2012-11-19
14:53:48
上传
评论
收藏 49KB RAR 举报
温馨提示
android 开发实例 1、 图片浏览器(Picture Viewer) 2 、文件浏览器(File Browser) 3 、通讯录(Contact) 4 、任务管理器(Task Manager) 5 、音乐播放器(Rock Player) 6 、天气预报(Android Weather Forecast) 7 、多媒体播放器(Android Media Player) 8、短信语音播报(Speak Message Service) 9、手机远程监控(Cam Monitor) 10、贪吃蛇游戏(Snake) -android development example 1, photo viewer (Picture Viewer) 2, file browser (File Browser) 3, Contacts (Contact) 4, Task Manager (Task Manager) 5, music player (Rock Player) 6, weather forecast (Android Weather Forecast) 7, multimedia player (Android Media Player) 8, SMS voice broadcast (Speak Message Service) 9, cell phone remote control (Cam Monitor) 10, Snake (Snake)
资源推荐
资源详情
资源评论
收起资源包目录
2_filebrowser.rar (33个子文件)
filebrowser
.project 862B
src
com
xmobileapp
filebrowser
iconifiedlist
IconifiedText.java 2KB
IconifiedTextView.java 2KB
IconifiedTextListAdapter.java 2KB
FileBrowser.java 6KB
AndroidManifest.xml 690B
res
drawable
webtext.png 1KB
packed.png 1KB
icon.png 667B
goroot.png 1KB
uponelevel.png 1KB
text.png 971B
audio.png 964B
image.png 1KB
folder.png 1015B
layout
values
fileendings.xml 694B
strings.xml 205B
assets
gen
com
xmobileapp
filebrowser
R.java 1KB
default.properties 960B
.classpath 280B
bin
resources.ap_ 9KB
com
xmobileapp
filebrowser
R$array.class 547B
R$drawable.class 665B
R$attr.class 364B
iconifiedlist
IconifiedText.class 2KB
IconifiedTextListAdapter.class 3KB
IconifiedTextView.class 2KB
FileBrowser$DISPLAYMODE.class 1KB
FileBrowser.class 6KB
R.class 535B
R$string.class 493B
classes.dex 11KB
com.xmobileapp.filebrowser.apk 17KB
共 33 条
- 1
kuku19921
- 粉丝: 1
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页