package com.yarin.android.FileManager;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class FileManager extends ListActivity
{
private List<IconifiedText> directoryEntries = new ArrayList<IconifiedText>();
private File currentDirectory = new File("/");
private File myTmpFile = null;
private int myTmpOpt = -1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
browseToRoot();
this.setSelection(0);
}
//浏览文件系统的根目录
private void browseToRoot()
{
browseTo(new File("/"));
}
//返回上一级目录
private void upOneLevel()
{
if(this.currentDirectory.getParent() != null)
this.browseTo(this.currentDirectory.getParentFile());
}
//浏览指定的目录,如果是文件则进行打开操作
private void browseTo(final File file)
{
this.setTitle(file.getAbsolutePath());
if (file.isDirectory())
{
this.currentDirectory = file;
fill(file.listFiles());
}
else
{
fileOptMenu(file);
}
}
//打开指定文件
protected void openFile(File aFile)
{
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(aFile.getAbsolutePath());
// 取得文件名
String fileName = file.getName();
// 根据不同的文件类型来打开文件
if (checkEndsWithInStringArray(fileName, getResources().getStringArray(R.array.fileEndingImage)))
{
intent.setDataAndType(Uri.fromFile(file), "image/*");
}
else if (checkEndsWithInStringArray(fileName, getResources().getStringArray(R.array.fileEndingAudio)))
{
intent.setDataAndType(Uri.fromFile(file), "audio/*");
}
else if (checkEndsWithInStringArray(fileName, getResources().getStringArray(R.array.fileEndingVideo)))
{
intent.setDataAndType(Uri.fromFile(file), "video/*");
}
startActivity(intent);
}
//这里可以理解为设置ListActivity的源
private void fill(File[] files)
{
//清空列表
this.directoryEntries.clear();
//添加一个当前目录的选项
this.directoryEntries.add(new IconifiedText(getString(R.string.current_dir), getResources().getDrawable(R.drawable.folder)));
//如果不是根目录则添加上一级目录项
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();
//根据文件名来判断文件类型,设置不同的图标
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 if (checkEndsWithInStringArray(fileName, getResources().getStringArray(R.array.fileEndingVideo)))
{
currentIcon = getResources().getDrawable(R.drawable.video);
}
else
{
currentIcon = getResources().getDrawable(R.drawable.text);
}
}
//确保只显示文件名、不显示路径如:/sdcard/111.txt就只是显示111.txt
int currentPathStringLenght = this.currentDirectory.getAbsolutePath().length();
this.directoryEntries.add(new IconifiedText(currentFile.getAbsolutePath().substring(currentPathStringLenght), currentIcon));
}
Collections.sort(this.directoryEntries);
IconifiedTextListAdapter itla = new IconifiedTextListAdapter(this);
//将表设置到ListAdapter中
itla.setListItems(this.directoryEntries);
//为ListActivity添加一个ListAdapter
this.setListAdapter(itla);
}
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
// 取得选中的一项的文件名
String selectedFileString = this.directoryEntries.get(position).getText();
if (selectedFileString.equals(getString(R.string.current_dir)))
{
//如果选中的是刷新
this.browseTo(this.currentDirectory);
}
else if (selectedFileString.equals(getString(R.string.up_one_level)))
{
//返回上一级目录
this.upOneLevel();
}
else
{
File clickedFile = null;
clickedFile = new File(this.currentDirectory.getAbsolutePath()+ this.directoryEntries.get(position).getText());
if(clickedFile != null)
this.browseTo(clickedFile);
}
}
//通过文件名判断是什么类型的文件
private boolean checkEndsWithInStringArray(String checkItsEnd,
String[] fileEndings)
{
for(String aEnd : fileEndings)
{
if(checkItsEnd.endsWith(aEnd))
return true;
}
return false;
}
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
menu.add(0, 0, 0, "新建目录").setIcon(R.drawable.addfolderr);
menu.add(0, 1, 0, "删除目录").setIcon(R.drawable.delete);
menu.add(0, 2, 0, "粘贴文件").setIcon(R.drawable.paste);
menu.add(0, 3, 0, "根目录").setIcon(R.drawable.goroot);
menu.add(0, 4, 0, "上一级").setIcon(R.drawable.uponelevel);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch (item.getItemId())
{
case 0:
Mynew();
break;
case 1:
//注意:删除目录,谨慎操作,该例子提供了
//deleteFile(删除文件)和deleteFolder(删除整个目录)
MyDelete();
break;
case 2:
MyPaste();
break;
case 3:
this.browseToRoot();
break;
case 4:
this.upOneLevel();
break;
}
return false;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu)
{
return super.onPrepareOptionsMenu(menu);
}
//粘贴操作
public void MyPaste()
{
if ( myTmpFile == null )
{
Builder builder = new Builder(FileManager.this);
builder.setTitle("提示");
builder.setMessage("没有复制或剪切操作");
builder.setPositiveButton(android.R.string.ok,
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setCancelable(false);
builder.create();
builder.show();
}
else
{
if ( myTmpOpt == 0 )//复制操作
{
if(new File(GetCurDirectory()+"/"+myTmpFile.getName()).exists())
{
Builder builder = new Builder(FileManager.this);
builder.setTitle("粘贴提示");
builder.setMessage("该目录有相同的文件,是否需要覆盖?");
builder.setPositiveButton(android.R.string.ok,
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
copyFile(myTmpFile,new File(GetCurDirectory()+"/"+myTmpFile.getName()));
browse
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
7个经典的Android应用程序源代码 (274个子文件)
resources.ap_ 54KB
resources.ap_ 48KB
resources.ap_ 44KB
resources.ap_ 26KB
resources.ap_ 13KB
resources.ap_ 11KB
resources.ap_ 10KB
FileManager.apk 67KB
MyContacts.apk 60KB
RingProfile.apk 52KB
MobileMap.apk 36KB
CityWeather.apk 20KB
TodayDate.apk 17KB
MusicPlayer.apk 17KB
FileManager.class 14KB
MobileMap.class 9KB
RingProfile.class 9KB
ContactEditor.class 7KB
ContactsProvider.class 7KB
MyContacts.class 7KB
CityWeather.class 5KB
MusicList.class 5KB
ContactView.class 4KB
LocationOverlay.class 4KB
FileManager$11.class 4KB
GoogleWeatherHandler.class 4KB
MusicPlayerService.class 3KB
TodayDate.class 3KB
FileManager$11$1.class 3KB
ConstData.class 3KB
ConstData.class 3KB
TodayDateSmall.class 3KB
IconifiedTextListAdapter.class 2KB
SingleWeatherInfoView.class 2KB
RingBroadcastReceiver.class 2KB
FileManager$8.class 2KB
FileManager$11$4.class 2KB
MusicListAdapter.class 2KB
FileManager$11$1$1.class 2KB
WeatherCurrentCondition.class 2KB
RingProfile$6.class 2KB
RingProfile$7.class 2KB
MobileMap$2.class 2KB
MusicInfoController.class 2KB
CityWeather$1.class 2KB
RingProfile$1.class 2KB
IconifiedTextView.class 2KB
IconifiedText.class 2KB
RingProfile$10.class 2KB
RingProfile$9.class 2KB
RingProfile$8.class 2KB
WeatherForecastCondition.class 2KB
CityWeather$2.class 2KB
FileManager$4.class 2KB
MusicList$2.class 1KB
FileManager$2.class 1KB
DBHelper.class 1KB
FileManager$11$4$1.class 1KB
WeatherSet.class 1KB
MusicList$4.class 1KB
MobileMap$4.class 1KB
RingProfile$4.class 1KB
RingProfile$2.class 1KB
ContactEditor$1.class 1KB
MusicList$3.class 1KB
R$id.class 1KB
MusicList$1.class 1KB
MobileMap$3.class 1KB
MobileMap$1.class 1KB
R$id.class 1KB
R$id.class 1KB
ContactColumn.class 1KB
RingProfile$5.class 1KB
RingProfile$3.class 1KB
ContactEditor$2.class 1KB
MusicPlayerService$1.class 1023B
MusicPlayerService$2.class 1012B
FileManager$11$1$2.class 986B
FileManager$11$4$2.class 986B
FileManager$11$3.class 948B
R$string.class 920B
FileManager$11$5.class 918B
FileManager$11$2.class 918B
FileManager$8$1.class 913B
FileManager$8$2.class 913B
FileManager$7.class 906B
FileManager$6.class 906B
FileManager$3.class 905B
FileManager$1.class 905B
FileManager$5.class 905B
FileManager$9.class 903B
MobileMap$5.class 886B
FileManager$10.class 885B
MobileMap$6.class 866B
MusicPlayerApp.class 863B
R$drawable.class 796B
MusicPlayerService$LocalBinder.class 733B
R$drawable.class 700B
R.class 667B
R.class 667B
共 274 条
- 1
- 2
- 3
jiu_yexue
- 粉丝: 0
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页