Android递归方式删除某文件夹下的所有文件(.mp3文件等等)
程序员文章站
2023-11-04 14:40:40
1.由于需要删除文件,因此需要如下权限:
1.由于需要删除文件,因此需要如下权限:
<uses-permission android:name="android.permission.write_external_storage"></uses-permission>
2.核心代码
package com.example.deleteyoumi;
import java.io.file;
import android.os.bundle;
import android.os.handler;
import android.os.message;
import android.app.activity;
import android.view.menu;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.toast;
/*
*@author: zhenghaibo
*web: http://blog.csdn.net/nuptboyzhb
*mail: zhb931706659@126.com
*2013-4-28 nanjing,njupt,china
*/
public class deleteyoumimainactivity extends activity implements
onclicklistener {
private button deletebutton;
private final static string youmipathstring = "/mnt/sdcard/android/data/.youmicache/";//文件夹的路径
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_delete_you_mi_main);
deletebutton = (button) findviewbyid(r.id.delete_youmi);
deletebutton.setonclicklistener(this);
}
@override
public boolean oncreateoptionsmenu(menu menu) {
// inflate the menu; this adds items to the action bar if it is present.
getmenuinflater().inflate(r.menu.delete_you_mi_main, menu);
return true;
}
@override
public void onclick(view v) {
// todo auto-generated method stub
switch (v.getid()) {
case r.id.delete_youmi:
file file = new file(youmipathstring);
deletefile(file);
mhandler.sendemptymessage(1);
break;
default:
break;
}
}
handler mhandler = new handler() {
public void handlemessage(message msg) {
switch (msg.what) {
case 0:
toast.maketext(getapplicationcontext(), "文件或文件夹不存在", toast.length_long).show();
break;
case 1:
toast.maketext(getapplicationcontext(), "删除成功!", toast.length_long).show();
break;
default:
break;
}
};
};
/**
* 递归删除文件和文件夹
*
* @param file
* 要删除的根目录
*/
public void deletefile(file file) {
if (file.exists() == false) {
mhandler.sendemptymessage(0);
return;
} else {
if (file.isfile()) {
file.delete();
return;
}
if (file.isdirectory()) {
file[] childfile = file.listfiles();
if (childfile == null || childfile.length == 0) {
file.delete();
return;
}
for (file f : childfile) {
deletefile(f);
}
file.delete();
}
}
}
}
3.拓展
本文是以删除为例。当然,对于遍历某文件夹下的所有文件均可用这个方法。如搜索.mp3文件等。
<uses-permission android:name="android.permission.write_external_storage"></uses-permission>
2.核心代码
复制代码 代码如下:
package com.example.deleteyoumi;
import java.io.file;
import android.os.bundle;
import android.os.handler;
import android.os.message;
import android.app.activity;
import android.view.menu;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.toast;
/*
*@author: zhenghaibo
*web: http://blog.csdn.net/nuptboyzhb
*mail: zhb931706659@126.com
*2013-4-28 nanjing,njupt,china
*/
public class deleteyoumimainactivity extends activity implements
onclicklistener {
private button deletebutton;
private final static string youmipathstring = "/mnt/sdcard/android/data/.youmicache/";//文件夹的路径
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_delete_you_mi_main);
deletebutton = (button) findviewbyid(r.id.delete_youmi);
deletebutton.setonclicklistener(this);
}
@override
public boolean oncreateoptionsmenu(menu menu) {
// inflate the menu; this adds items to the action bar if it is present.
getmenuinflater().inflate(r.menu.delete_you_mi_main, menu);
return true;
}
@override
public void onclick(view v) {
// todo auto-generated method stub
switch (v.getid()) {
case r.id.delete_youmi:
file file = new file(youmipathstring);
deletefile(file);
mhandler.sendemptymessage(1);
break;
default:
break;
}
}
handler mhandler = new handler() {
public void handlemessage(message msg) {
switch (msg.what) {
case 0:
toast.maketext(getapplicationcontext(), "文件或文件夹不存在", toast.length_long).show();
break;
case 1:
toast.maketext(getapplicationcontext(), "删除成功!", toast.length_long).show();
break;
default:
break;
}
};
};
/**
* 递归删除文件和文件夹
*
* @param file
* 要删除的根目录
*/
public void deletefile(file file) {
if (file.exists() == false) {
mhandler.sendemptymessage(0);
return;
} else {
if (file.isfile()) {
file.delete();
return;
}
if (file.isdirectory()) {
file[] childfile = file.listfiles();
if (childfile == null || childfile.length == 0) {
file.delete();
return;
}
for (file f : childfile) {
deletefile(f);
}
file.delete();
}
}
}
}
3.拓展
本文是以删除为例。当然,对于遍历某文件夹下的所有文件均可用这个方法。如搜索.mp3文件等。