android 删除文件,打开指定的文件类型
来源:互联网 发布:多益网络账号格式错误 编辑:程序博客网 时间:2024/05/29 08:33
android 删除文件,打开指定的文件类型
public class FileUtils {
/**
* 删除指定文件
*
* @param fileNames
*/
public static void deleteFiles(String... fileNames) {
if (fileNames.length <= 0)
return;
for (int i = 0; i < fileNames.length; i++) {
File file = new File(fileNames[i]);
if (file.exists())
file.delete();
}
}
/**
* 删除指定文件夹下的所有文件
*
* @param path
* @return
*/
public static boolean delAllFile(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
long lastModeify = temp.lastModified();
long current = new Date().getTime();
Log.e("文件创建时间", "current=" + current + " lastModeify="
+ lastModeify);
if (current - lastModeify > (1000 * 60 * 60 * 24 * 3)) {
temp.delete();
}
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
flag = true;
}
}
return flag;
}
/**
* 文件是否存在
*
* @param nativePath
* @return
*/
public static boolean isExists(String nativePath) {
File file = new File(nativePath);
return file.exists();
}
/**
* 打开文件的方式
*
* @param f文件对象
*/
public static void openFile(File f, Context context) {
if (!f.exists()) {
Toast.makeText(context, "文件尚未下载,无法查看,请您先下载文件!", Toast.LENGTH_SHORT)
.show();
return;
}
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
/* 调用getMIMEType()来取得MimeType */
String type = getMIMEType(f);
/* 设置intent的file与MimeType */
intent.setDataAndType(Uri.fromFile(f), type);
context.startActivity(intent);
}
/* 判断文件MimeType的method */
private static String getMIMEType(File f) {
String type = "";
String fName = f.getName();
/* 取得扩展名 */
String end = fName
.substring(fName.lastIndexOf(".") + 1, fName.length())
.toLowerCase();
/* 依扩展名的类型决定MimeType */
if (end.equals("m4a") || end.equals("mp3") || end.equals("mid")
|| end.equals("xmf") || end.equals("ogg") || end.equals("wav")
|| end.equals("wma")) {
type = "audio";
} else if (end.equals("3gp") || end.equals("mp4")) {
type = "video";
} else if (end.equals("jpg") || end.equals("gif") || end.equals("png")
|| end.equals("jpeg") || end.equals("bmp")) {
type = "image";
} else if (end.equals("apk")) {
/* android.permission.INSTALL_PACKAGES */
type = "application/vnd.android.package-archive";
} else {
type = "*";
}
/* 如果无法直接打开,就跳出软件列表给用户选择 */
if (end.equals("apk")) {
} else {
type += "/*";
}
return type;
}
- android 删除文件,打开指定的文件类型
- 删除一种文件类型的文件
- android 指定文件打开的方式
- input 文件上传指定特定的文件类型
- 如何打开不知道文件类型的文件
- 打开指定的文件
- .NET删除过期的临时文件---指定路径,文件类型,过期时间
- 删除指定的文件
- Android 删除指定文件代码
- grep 命令(在指定文件的文件类型中查找)
- 文件上传时只接收指定的文件类型
- 设置打开或保存文件对话框的文件类型
- 文件后缀和相应的文件类型,打开方法参考大全
- AS中文件类型的转换问题(文件打开方式)
- android打开指定的浏览器
- wince在指定的路径下 打开,读,创建,写,删除txt文件
- Android菜鸟笔记-关联APK程序打开的文件类型:MimeType
- android上获得打开相应文件类型的intent
- Json后台转换
- IP首部结构及组包算法分析
- htonl htons ntohs ntohl四个函数的粗浅理解
- 深入浅出MFC笔记2-MFC程序如何包装Win32程序
- bugzilla的report中文乱码问题
- android 删除文件,打开指定的文件类型
- VS2010下Team Foundation Server连接不上
- linux shell 字符串操作(长度,查找,替换)详解
- 使用WakeLock使Android应用程序保持后台唤醒
- Linux系列-文件系统基本结构和文件基本操作管理
- 开源游戏学习- 模拟系统的架构设计
- SQL Server 2008编程入门经典笔记(第十四章:事务和锁)
- Linux Memory Policy
- Java -- 泛型