先看看效果图
直接上代码吧:
package com.example.managedownfile;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.TextView;
public class ManageDownFile extends Activity {
private static String rootPath = "/xxxxx";
ListView lv;
private List<String> items = null;//存放名称
private List<String> paths = null;//存放路径
ListItem listitem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.managedownfile);
rootPath = Environment.getExternalStorageDirectory().getAbsolutePath()+rootPath;
lv = (ListView) findViewById(R.id.downfilemanagerlist);
getFileDir(rootPath) ;
}
class ListItem
{
List<Map<String ,Object>> listitems;
int count;
public ListItem() {
count = 0;
listitems = new ArrayList<Map<String,Object>>();
}
}
public static Drawable getApkIcon(Context context, String apkPath) {
PackageManager pm = context.getPackageManager();
PackageInfo info = pm.getPackageArchiveInfo(apkPath,PackageManager.GET_ACTIVITIES);
if (info != null) {
ApplicationInfo appInfo = info.applicationInfo;
appInfo.sourceDir = apkPath;
appInfo.publicSourceDir = apkPath;
try {
Log.i("sss", "sddddddddd");
return appInfo.loadIcon(pm);
} catch (OutOfMemoryError e) {
Log.e("ApkIconLoader", e.toString());
}
}
return null;
}
String getFileSize(File f)
{
String size = "未知大小";
int filelen = 0;
FileInputStream fis = null;
try {
fis = new FileInputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
filelen = fis.available();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int fl = 0;
if(filelen>0)
{
if(filelen>1024)
{//k
fl = filelen/1024;
if(filelen>1024*1024)
{//M
fl = (int) ((filelen/1024.0)/1024.0);
size = fl +"";//整数位
fl = (int) (((filelen/1024.0)*1000)/1024.0-fl*1000);//小数前三位
if(fl%10>4)
fl = fl/10+1;
else
fl = fl/10;
size = size+"."+fl+"MB";
}
else
{
size = fl +"";//整数位
fl = (filelen*100)/1024-fl*100;//小数前两位
size = size+"."+fl+"KB";
}
}
else
size = filelen +"B";
}
return size;
}
public void getFileDir(String filePath) {
try{
File f = new File(filePath);
if(!f.exists())
f.mkdirs();
File[] files = f.listFiles();// 列出所有文件
if(files != null)
{
listitem = new ListItem();
listitem.count = files.length;// 文件个数
for (int i = 0; i < listitem.count; i++)
{
Map<String ,Object> item = new HashMap<String, Object>();
item.put("icon", getApkIcon(this,files[i].getPath()));
item.put("filename", files[i].getName());
item.put("filesize", getFileSize(files[i]));
item.put("filepath", files[i].getPath());
listitem.listitems.add(item);
}
}
SimpleAdapter sadapter = new SimpleAdapter(this, listitem.listitems, R.layout.fordownfilemanage,
new String[]{"icon","filename","filesize","filepath"}, new int[] {R.id.fileicon,R.id.filename,R.id.filesize,R.id.filepath});
lv.setAdapter(sadapter);
sadapter.setViewBinder(new ViewBinder(){
public boolean setViewValue(View view,Object data,String textRepresentation){
if(view instanceof ImageView && data instanceof Drawable){
ImageView iv=(ImageView)view;
iv.setImageDrawable((Drawable)data);
return true;
}
else return false;
}
});
}catch(Exception ex){
Log.i("ss",ex.toString());
}
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
// TODO Auto-generated method stub
Log.i("aaaaaaaa", ""+((TextView)arg1.findViewById(R.id.filename)).getText());
openfile(((TextView)arg1.findViewById(R.id.filepath)).getText().toString());
}
});
}
void openfile(String filePath)
{
/* 取得扩展名 */
String end=filePath.substring(filePath.lastIndexOf(".") + 1).toLowerCase();
Intent intent;
/* 依扩展名的类型决定MimeType */
if(end.equals("m4a")||end.equals("mp3")||end.equals("mid")||
end.equals("xmf")||end.equals("ogg")||end.equals("wav")){
intent = getAudioFileIntent(filePath);
}else if(end.equals("3gp")||end.equals("mp4")){
intent = getAudioFileIntent(filePath);
}else if(end.equals("jpg")||end.equals("gif")||end.equals("png")||
end.equals("jpeg")||end.equals("bmp")){
intent = getImageFileIntent(filePath);
}else if(end.equals("apk")){
intent = getApkFileIntent(filePath);
}else if(end.equals("ppt")){
intent = getPptFileIntent(filePath);
}else if(end.equals("xls")){
intent = getExcelFileIntent(filePath);
}else if(end.equals("doc")){
intent = getWordFileIntent(filePath);
}else if(end.equals("pdf")){
intent = getPdfFileIntent(filePath);
}else if(end.equals("chm")){
intent = getChmFileIntent(filePath);
}else if(end.equals("txt")){
intent = getTextFileIntent(filePath,false);
}else{
intent = getAllIntent(filePath);
}
startActivity(intent);
}
//Android获取一个用于打开APK文件的intent
public static Intent getAllIntent( String param ) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(param ));
intent.setDataAndType(uri,"*/*");
return intent;
}
//Android获取一个用于打开APK文件的intent
public static Intent getApkFileIntent( String param ) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(param ));
intent.setDataAndType(uri,"application/vnd.android.package-archive");
return intent;
}
//Android获取一个用于打开VIDEO文件的intent
public static Intent getVideoFileIntent( String param ) {
Intent intent = new Intent("android.intent.action.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("oneshot", 0);
intent.putExtra("configchange", 0);
Uri uri = Uri.fromFile(new File(param ));
intent.setDataAndType(uri, "video/*");
return intent;
}
//Android获取一个用于打开AUDIO文件的intent
public static Intent getAudioFileIntent( String param ){
Intent intent = new Intent("android.intent.action.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("oneshot", 0);
intent.putExtra("configchange", 0);
Uri uri = Uri.fromFile(new File(param ));
intent.setDataAndType(uri, "audio/*");
return intent;
}
//Android获取一个用于打开Html文件的intent
public static Intent getHtmlFileIntent( String param ){
Uri uri = Uri.parse(param ).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(param ).build();
Intent intent = new Intent("android.intent.action.VIEW");
intent.setDataAndType(uri, "text/html");
return intent;
}
//Android获取一个用于打开图片文件的intent
public static Intent getImageFileIntent( String param ) {
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(new File(param ));
intent.setDataAndType(uri, "image/*");
return intent;
}
//Android获取一个用于打开PPT文件的intent
public static Intent getPptFileIntent( String param ){
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(new File(param ));
intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
return intent;
}
//Android获取一个用于打开Excel文件的intent
public static Intent getExcelFileIntent( String param ){
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(new File(param ));
intent.setDataAndType(uri, "application/vnd.ms-excel");
return intent;
}
//Android获取一个用于打开Word文件的intent
public static Intent getWordFileIntent( String param ){
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(new File(param ));
intent.setDataAndType(uri, "application/msword");
return intent;
}
//Android获取一个用于打开CHM文件的intent
public static Intent getChmFileIntent( String param ){
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(new File(param ));
intent.setDataAndType(uri, "application/x-chm");
return intent;
}
//Android获取一个用于打开文本文件的intent
public static Intent getTextFileIntent( String param, boolean paramBoolean){
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (paramBoolean){
Uri uri1 = Uri.parse(param );
intent.setDataAndType(uri1, "text/plain");
}else{
Uri uri2 = Uri.fromFile(new File(param ));
intent.setDataAndType(uri2, "text/plain");
}
return intent;
}
//Android获取一个用于打开PDF文件的intent
public static Intent getPdfFileIntent( String param ){
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(new File(param ));
intent.setDataAndType(uri, "application/pdf");
return intent;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/fileicon"
android:layout_margin="5dp"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="4"
android:src="@drawable/ic_launcher"/>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:id="@+id/filename"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="文件名"
/>
<TextView
android:id="@+id/filesize"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="文件大小"
/>
<TextView
android:id="@+id/filepath"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="文件大小"
android:visibility="gone"
/>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/downfilemanagerlist"
android:layout_width="fill_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
参考了以下博文
http://blog.csdn.net/weich_java/article/details/7002829 ,还有一些其他资料,也表示感谢
还有,推荐一个资源,别人写的,我下载看了,还没来得及仔细研究,但是感觉很强大,
这是下载链接http://download.csdn.net/detail/kangkangxuxu/3434346
另外推荐看郭霖的博客 http://blog.csdn.net/sinyu890807/
新的版本:增加了删除功能,博客中没有修改,