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)
{
System.out.println(file.getAbsolutePath());
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(GetCurDirec
没有合适的资源?快使用搜索试试~ 我知道了~
文件管理 Android 文件操作 列表显示 进入 退出 删除 复制 粘贴 新建文件等等(程序源码).rar
共79个文件
class:34个
png:26个
xml:6个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 109 浏览量
2023-03-17
08:58:26
上传
评论
收藏 265KB RAR 举报
温馨提示
免责声明:资料部分来源于合法的互联网渠道收集和整理,部分自己学习积累成果,供大家学习参考与交流。收取的费用仅用于收集和整理资料耗费时间的酬劳。 本人尊重原创作者或出版方,资料版权归原作者或出版方所有,本人不对所涉及的版权问题或内容负法律责任。如有侵权,请举报或通知本人删除。
资源推荐
资源详情
资源评论
收起资源包目录
文件管理 Android 文件操作 列表显示 进入 退出 删除 复制 粘贴 新建文件等等(程序源码).rar (79个子文件)
com.yarin.android.FileManager.FileManager
project.properties 648B
.classpath 364B
.settings
org.eclipse.jdt.core.prefs 177B
assets
src
com
yarin
android
FileManager
FileManager.java 20KB
IconifiedText.java 1KB
IconifiedTextView.java 1KB
IconifiedTextListAdapter.java 2KB
res
drawable-mdpi
drawable-xhdpi
drawable-ldpi
values
fileendings.xml 862B
strings.xml 241B
layout
rename.xml 605B
dialog.xml 611B
drawable
folder.png 2KB
image.png 2KB
text.png 2KB
paste.png 3KB
uponelevel.png 3KB
goroot.png 2KB
audio.png 2KB
video.png 2KB
delete.png 3KB
addfolderr.png 3KB
webtext.png 2KB
packed.png 2KB
icon.png 22KB
drawable-hdpi
bin
classes
com
yarin
android
FileManager
FileManager$8$2.class 913B
FileManager$5.class 905B
FileManager$11$1.class 3KB
FileManager$8$1.class 913B
FileManager$7.class 906B
FileManager$11$4.class 2KB
FileManager$11$1$2.class 986B
FileManager$11$3.class 948B
FileManager$3.class 905B
FileManager$11$1$1.class 2KB
IconifiedTextListAdapter.class 2KB
R$string.class 502B
FileManager$9.class 903B
R$layout.class 459B
FileManager$2.class 1KB
FileManager$8.class 2KB
R$id.class 529B
R$array.class 595B
FileManager$11$4$1.class 1KB
BuildConfig.class 367B
IconifiedText.class 2KB
R$attr.class 373B
FileManager$11$5.class 918B
FileManager$11.class 4KB
R$drawable.class 796B
FileManager$11$2.class 918B
FileManager$10.class 885B
FileManager$4.class 2KB
IconifiedTextView.class 2KB
FileManager$1.class 905B
R.class 667B
FileManager$11$4$2.class 986B
FileManager.class 13KB
FileManager$6.class 906B
resources.ap_ 53KB
res
drawable
folder.png 1KB
image.png 2KB
text.png 2KB
paste.png 3KB
uponelevel.png 3KB
goroot.png 2KB
audio.png 2KB
video.png 2KB
delete.png 3KB
addfolderr.png 3KB
webtext.png 2KB
packed.png 2KB
icon.png 20KB
classes.dex 25KB
AndroidManifest.xml 839B
com.yarin.android.FileManager.FileManager.apk 67KB
.project 847B
AndroidManifest.xml 839B
gen
com
yarin
android
FileManager
BuildConfig.java 171B
R.java 2KB
共 79 条
- 1
资源评论
金枝玉叶9
- 粉丝: 137
- 资源: 7640
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 微信小程序 - 图书管理系统源码.zip
- 微信小程序 - 图片自适应 ,富文本解析源码.zip
- 微信小程序 - 同乐居商城:购物车合算源码
- 1、根据输入的三条边值判断能组成何种三角形,并设计测试数据进行判定覆盖测试 三条边为变量a、b、c,范围为1≤边值≤10,不在范
- SQL server 练习题目8道(小白教学).zip
- Python 手写实现 iD3 决策树算法-根据信息增益公式.zip
- 411675952289057车联助手-小窗版(三星)3.5.1.apk
- 三种快速排序方法合并在一个文件中以便直接运行的Python代码示例
- 937712277954201实习5.word
- 2程序语言基础知识pdf1_1716337722703.jpeg
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功