实例介绍
【实例简介】
实验描述:
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();
}
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)