Android 读取sdcard指定目录文件

读取android目录指定文件,通过该类可读取指定sdCard目录中的文件,对文件进行重命名,查看属性,显示文件大小,删除,打开操作,主要代码我已经加了注释,不妨各位自己看看,我就不多说了,代码如下:


import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cykj.oa.app.R;
import com.cykj.oa.app.adapter.FileListAdapter;
import com.cykj.oa.app.bean.FileBean;
import com.cykj.oa.app.utils.FileUtil;



public class DocumentManage extends ListActivity implements OnItemLongClickListener, OnItemSelectedListener, OnItemClickListener {
	String SDPATH = Environment.getExternalStorageDirectory() + "/OA-APP/";
	private static final String[] operate = { "删除", "重命名", "属性" };
	private View newAlertView;
	private EditText newFileName_EditText;
	private static String currentDirctory = "/mnt/sdcard/APP"; 
	ScanFileTask scanFileTask;//扫描文件的任务
	private List<FileBean> files = new ArrayList<FileBean>();//FileBean有两个属性filename,filerul
	private boolean isListViewShow = true;
	private FileListAdapter fileListAdapter;
	private int currentItem = 0;
	private boolean out=false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.listview_documentmanage);
		
		if (checkSDCard()) {// 加载文件列表
			fileListAdapter = new FileListAdapter(this, files, FileUtil.isZoom);
			setListAdapter(fileListAdapter);

			getListView().setOnItemLongClickListener(this);//长按显示上下文菜单
			getListView().setOnItemSelectedListener(this);//指的是单击一个条目
			getListView().setOnItemClickListener(this);
			scanFile(FileUtil.rootPath);//文件扫描
		}
	}

	
	/* 检查SDCard是否挂载 */
	public boolean checkSDCard() {
		if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			showMessage(getStr(com.cykj.oa.app.R.string.no_sdcard));
			return false;
		}
		return true;
	}
	
	
	// 短消息提示
		public void showMessage(String text) {
			Toast.makeText(this, text, Toast.LENGTH_LONG).show();
		}

		// 从资源获取字符串
		public String getStr(int ID) {
			return getResources().getString(ID);
		}

		/* 文件打开处理 */
		public void openFile(File f) {
			Intent intent = new Intent();

			// 采用系统默认打开方式
			//Log.i(TAG, "type123="+FileUtil.isOpen );
			//FileUtil.isOpen=1;
			if (FileUtil.isOpen == 0) {
				String type = FileUtil.getMIMEType(f);
				// 打开图片浏览器
				if (type.equals("image")) {
//					intent.setClass(MainActivity.this, BrowseImagesActivity.class);
					intent.putStringArrayListExtra("images", FileUtil.searchImages(f));
					startActivity(intent);
				}

				// 打开媒体播放器 
				
				else if (type.equals("audio") || type.equals("video")) {
//					intent.setClass(MainActivity.this, MediaPlayerActivity.class);
			       
			        
					Bundle bl = new Bundle();
					bl.putString("mediapath", f.getAbsolutePath());
					bl.putString("mediatype", type);
					intent.putExtras(bl);
					startActivity(intent);
					
					
				}
				else if(type.equals("txt")){
					intent = new Intent("android.intent.action.VIEW");  
					intent.addCategory("android.intent
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值