【实例简介】
实验描述:
Android系统并不自带文件管理器,但是很多情况下,我们有从SD卡中打开文件的需要,怎么办?相信大家都比较习惯Windows下操作文件和文件夹的方式,那么Android下是否也有类似的工具呢?答案是必须有。本次实验我们将要开发的应用就是Android平台下的文件管理器。
基于Android平台的文件管理器,我们借鉴Windows,从用户实际使用需求出发,主要实现的功能有:
1)浏览任意目录下的文件及文件夹
2)打开文件
3)新建文件
4)删除文件
5)复制文件
6)对文件重命名
7)在当前目录或者整个目录进行搜索
8)返回上一级以及根目录。
【实例截图】
【核心代码】
/**注册广播*/
private IntentFilter filter;
private FileBroadcast fileBroadcast;
private IntentFilter intentFilter;
private SearchBroadCast serviceBroadCast;
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
filter = new IntentFilter();
filter.addAction(FileService.FILE_SEARCH_COMPLETED);
filter.addAction(FileService.FILE_NOTIFICATION);
intentFilter = new IntentFilter();
intentFilter.addAction(KEYWORD_BROADCAST);
if(fileBroadcast == null){
fileBroadcast = new FileBroadcast();
}
if(serviceBroadCast == null){
serviceBroadCast = new SearchBroadCast();
}
this.registerReceiver(fileBroadcast, filter);
this.registerReceiver(serviceBroadCast, intentFilter);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
fileNames.clear();
filePaths.clear();
this.unregisterReceiver(fileBroadcast);
this.unregisterReceiver(serviceBroadCast);
}
private String action;
class FileBroadcast extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
action = intent.getAction();
if(FileService.FILE_SEARCH_COMPLETED.equals(action)){
fileNames = intent.getStringArrayListExtra("fileNamesList");
filePaths = intent.getStringArrayListExtra("filePathsList");
Toast.makeText(MainActivity.this, "搜索完毕!", Toast.LENGTH_SHORT).show();
//这里搜索完毕之后应该弹出一个弹出框提示用户要不要显示数据
searchCompletedDialog("搜索完毕,是否马上显示结果?");
getApplicationContext().stopService(serviceIntent);//当搜索完毕的时候停止服务,然后在服务中取消通知
// 点击通知栏跳转过来的广播
}else if(FileService.FILE_NOTIFICATION.equals(action)){//点击通知回到当前Activity,读取其中信息
String mNotification = intent.getStringExtra("notification");
Toast.makeText(MainActivity.this, mNotification, Toast.LENGTH_LONG).show();
searchCompletedDialog("你确定要取消搜索吗?");
}
}
}
//搜索完毕和点击通知过来时的提示框
private void searchCompletedDialog(String message){
Builder searchDialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("提示")
.setMessage(message)
.setPositiveButton("确定", new OnClickListener(){
public void onClick(DialogInterface dialog,int which) {
//当弹出框时,需要对这个确定按钮进行一个判断,因为要对不同的情况做不同的处理(2种情况)
// 1.搜索完毕
// 2.取消搜索
if(FileService.FILE_SEARCH_COMPLETED.equals(action)){
if(fileNames.size() == 0){
Toast.makeText(MainActivity.this, "无相关文件/文件夹!", Toast.LENGTH_SHORT).show();
setListAdapter(new FileAdapter(MainActivity.this,fileNames,filePaths));//清空列表
}else{
//显示文件列表
setListAdapter(new FileAdapter(MainActivity.this,fileNames,filePaths));
}
}else{
//设置搜索标志为true,
isComeBackFromNotification = true;
//关闭服务,取消搜索
getApplicationContext().stopService(serviceIntent);
}
}
})
.setNegativeButton("取消", null);
searchDialog.create();
searchDialog.show();
}
public void paste(){
final String filePath1 = oriFilePath;
final String filePath2 = currentPath File.separator oriFileName;
if(isCopy&&!filePath1.equals(filePath2)){
if(!new File(filePath2).exists()){
copyFile(filePath1,filePath2);
traversalFileList(currentPath);
}else{
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
copyFile(filePath1, filePath2);
traversalFileList(currentPath);
Toast.makeText(getApplicationContext(), "文件覆盖成功!", 1000).show();
}
};
new AlertDialog.Builder(MainActivity.this)
.setTitle("温馨提示")
.setMessage("文件已经存在,是否覆盖?")
.setPositiveButton("确定", listener)
.setNeutralButton("取消", null).show();
}
}else{
Toast.makeText(getApplicationContext(), "请先复制文件!", 1000).show();
}
}
public void copyFile(String filePath1,String filePath2){
try {
FileInputStream fin = new FileInputStream(new File(filePath1));
FileOutputStream fout = new FileOutputStream(new File(filePath2));
int index;
while((index = fin.read())!=-1){
fout.write(index);
}
traversalFileList(currentPath);
if(fin!=null){
fin.close();
}
if(fout!=null){
fout.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void createFile(){
fileCheckedId = 2;
final LayoutInflater ll = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final LinearLayout layout = (LinearLayout)ll.inflate(R.layout.create_dialog,null);
radioGroup1 = (RadioGroup)layout.findViewById(R.id.radioGroup1);
final RadioButton createFileButton = (RadioButton)layout.findViewById(R.id.createFile);
final RadioButton createFolderButton = (RadioButton)layout.findViewById(R.id.createFolder);
createFolderButton.setChecked(true);
radioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(checkedId == createFileButton.getId()){
fileCheckedId = 1;
}else if(checkedId == createFolderButton.getId()){
fileCheckedId = 2;
}
}
});
Builder builder = new AlertDialog.Builder(MainActivity.this)
.setView(layout)
.setTitle("新建")
.setPositiveButton("创建", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
fileNameEditText = (EditText)layout.findViewById(R.id.createFileName);
createFileNameStr = fileNameEditText.getText().toString();
if(fileCheckedId == 1){
try {
createFile = new File(currentPath File.separator createFileNameStr ".txt");
createFile.createNewFile();
traversalFileList(currentPath);
} catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(MainActivity.this, "文件拼接出错", 1000).show();
}
}else if(fileCheckedId == 2){
createFile = new File(currentPath File.separator createFileNameStr);
if(!createFile.exists()&&!createFile.isDirectory()&&createFileNameStr.length()!=0){
if(createFile.mkdirs()){
traversalFileList(currentPath);
}else{
Toast.makeText(MainActivity.this, "创建失败,可能是系统权限不够,root一下?!", 1000).show();
}
}else{
Toast.makeText(MainActivity.this, "文件名为空,还是重名了呢?", 1000).show();
}
}
}
}).setNeutralButton("取消", null);
builder.show();
}