代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
android文件管理
作者:
舞维天下
/ 发布于
2013/7/19
/
971
package com.Syl.filemanager; import java.io.File; import java.util.ArrayList; import java.util.Currency; import java.util.HashMap; import java.util.List; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.view.KeyEvent; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; @SuppressLint("NewApi") public class MainActivity extends Activity { private List<String> paths=null; private String rootPath="/"; private TextView path; private ListView lv; private String backPath = ""; private long firstTime = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* 设定Layout activity_main.xml*/ if (isSDAvailable ()) { setContentView(R.layout.activity_main); path = (TextView)findViewById(R.id.filePath); lv = (ListView)findViewById(R.id.list); backPath = getSDPath(); rootPath = getSDPath(); getFileDir(rootPath); } else { setContentView(R.layout.no_sd); } } private void getFileDir(String filePath) { path.setText(filePath); backPath = filePath; System.out.println("filePath:"+filePath); paths = new ArrayList<String>(); File f = new File(filePath); File[] files=f.listFiles(); if (files == null) { return; } ArrayList<HashMap<String, Object>> file_list = new ArrayList<HashMap<String,Object>>(); if(!filePath.equals(rootPath)) { for (int i = 0; i < 2; i++) { if (i == 0) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("list_image", null); map.put("list_text", getString(R.string.back_to) + rootPath); file_list.add(map); paths.add(rootPath); } else { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("list_image", null); map.put("list_text", getString(R.string.back_to_parent)); file_list.add(map); paths.add(f.getParent()); } } } //将文件放入ArrayList數组中 for (int i = 0; i < files.length; i++) { HashMap<String, Object> map = new HashMap<String, Object>(); File file=files[i]; if (file.canRead()) { if (file.isDirectory()) { map.put("list_image", R.drawable.fileicon); }else { //不同文件类型设置不同icon String end=file.getName().substring(file.getName().lastIndexOf(".") +1,file.getName().length()).toLowerCase(); if (end.equals("jpg")||end.equals("gif")||end.equals("png")|| end.equals("jpeg")||end.equals("bmp")) { map.put("list_image", R.drawable.pic); }else if (end.equals("mp3")||end.equals("mid")||end.equals("wav")) { map.put("list_image", R.drawable.myfiles_file_mp3_b); }else if (end.equals("3gp")||end.equals("mp4")||end.equals("rmvb")) { map.put("list_image", R.drawable.myfiles_icon_video); }else if (end.equals("apk")) { map.put("list_image", R.drawable.myfiles_file_apk_b); }else if (end.equals("txt")||end.equals("c")||end.equals("java")||end.equals("xml")|| end.equals("cpp")) { map.put("list_image", R.drawable.myfiles_file_txt); }else { map.put("list_image", R.drawable.other_file); } } }else{ map.put("list_image", R.drawable.other_file); } map.put("list_text", file.getName()); file_list.add(map); paths.add(file.getPath()); } SimpleAdapter listItemAdapter = new SimpleAdapter(this, file_list, R.layout.file_item, new String[] {"list_image", "list_text"}, new int[] {R.id.imageItem,R.id.textItem}); lv.setAdapter(listItemAdapter); lv.setOnItemClickListener(new OnFileManagerItemClickListener()); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if (isSDAvailable ()) { long secTime = System.currentTimeMillis(); System.out.println("getSDPath():"+getSDPath()); System.out.println("backPath:"+backPath); System.out.println("secTime - firstTime="+(secTime - firstTime)); if (KeyEvent.KEYCODE_BACK == keyCode) { if (!backPath.equals(getSDPath())) { getFileDir((new File(backPath)).getParent()); } else { if (secTime - firstTime <= 2500) { MainActivity.this.finish(); System.exit(0); } else { Toast.makeText(MainActivity.this, R.string.double_back, Toast.LENGTH_SHORT).show(); firstTime = System.currentTimeMillis(); return true; } } } return false; } else { return super.onKeyUp(keyCode, event); } } private class OnFileManagerItemClickListener implements OnItemClickListener { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub File file = new File(paths.get(arg2)); if(file.canRead()) { if (file.isDirectory()) { getFileDir(paths.get(arg2)); }else { openSelectFile(file); } } else { /* 弹出AlertDialog提示*/ new AlertDialog.Builder(MainActivity.this) .setTitle(R.string.friendly_tip) .setMessage(R.string.file_cant_read) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int which) { dialog.dismiss(); } }).show(); } } } private boolean isSDAvailable() { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { return true; } return false; } private String getSDPath() { String path = Environment.getExternalStoragePublicDirectory("").toString(); if (path == null) { path = "/"; } return path; } private void openSelectFile(File f) { Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(android.content.Intent.ACTION_VIEW); String type = getFileType(f); intent.setDataAndType(Uri.fromFile(f),type); startActivity(intent); } /** * 判断文件类型 **/ private String getFileType(File f) { String type=""; String fName=f.getName(); /* 取得扩展名 */ String end=fName.substring(fName.lastIndexOf(".")+1,fName.length()).toLowerCase(); /* 依扩展名的类型决定FileType */ if(end.equals("mp3")||end.equals("mid")||end.equals("wav")||end.equals("aac")|| end.equals("amr")) { type = "audio"; } else if(end.equals("3gp")||end.equals("mp4")||end.equals("rmvb")) { 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 if(end.equals("txt")||end.equals("c")||end.equals("java")||end.equals("xml")|| end.equals("cpp")) { type = "text"; } else { type="*"; } /*若无法直接打开文件,就弹出软件列表*/ if(end.equals("apk")) { } else { type += "/*"; } return type; } }
试试其它关键字
文件管理
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
舞维天下
贡献的其它代码
(
4
)
.
基于控制台的RPG游戏实现
.
Java NIO网络编程
.
android文件管理
.
微信公众平台API的Java通讯实现
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3