原文地址:http://blog.csdn.net/trbbadboy/article/details/7899424;
因项目需要打开文件,因此做了一个打开文件的对话框,现在把这部分的代码共享出来了。
首先是一个回调接口,该接口在文件选择完毕的通知调用者进行如果何种操作。文件接口声明,如下:
-
-
package com.example.openfiledemo;
-
-
import android.os.Bundle;
-
-
public interface CallbackBundle {
-
abstract void callback(Bundle bundle);
-
}
然后的打开文件对话框的一下封装:
-
-
package com.example.openfiledemo;
-
-
import java.io.File;
-
import java.util.ArrayList;
-
import java.util.HashMap;
-
import java.util.List;
-
import java.util.Map;
-
-
import android.app.Activity;
-
import android.app.AlertDialog;
-
import android.app.Dialog;
-
import android.content.Context;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.widget.AdapterView;
-
import android.widget.ListView;
-
import android.widget.SimpleAdapter;
-
import android.widget.Toast;
-
import android.widget.AdapterView.OnItemClickListener;
-
-
public class OpenFileDialog {
-
public static String tag = "OpenFileDialog";
-
static final public String sRoot = "/";
-
static final public String sParent = "..";
-
static final public String sFolder = ".";
-
static final public String sEmpty = "";
-
static final private String sOnErrorMsg = "No rights to access!";
-
-
-
-
-
-
-
-
-
-
-
-
-
-
public static Dialog createDialog(int id, Context context, String title, CallbackBundle callback, String suffix, Map<String, Integer> images){
-
AlertDialog.Builder builder = new AlertDialog.Builder(context);
-
builder.setView(new FileSelectView(context, id, callback, suffix, images));
-
Dialog dialog = builder.create();
-
-
dialog.setTitle(title);
-
return dialog;
-
}
-
-
static class FileSelectView extends ListView implements OnItemClickListener{
-
-
-
private CallbackBundle callback = null;
-
private String path = sRoot;
-
private List<Map<String, Object>> list = null;
-
private int dialogid = 0;
-
-
private String suffix = null;
-
-
private Map<String, Integer> imagemap = null;
-
-
public FileSelectView(Context context, int dialogid, CallbackBundle callback, String suffix, Map<String, Integer> images) {
-
super(context);
-
this.imagemap = images;
-
this.suffix = suffix==null?"":suffix.toLowerCase();
-
this.callback = callback;
-
this.dialogid = dialogid;
-
this.setOnItemClickListener(this);
-
refreshFileList();
-
}
-
-
private String getSuffix(String filename){
-
int dix = filename.lastIndexOf(‘.‘);
-
if(dix<0){
-
return "";
-
}
-
else{
-
return filename.substring(dix+1);
-
}
-
}
-
-
private int getImageId(String s){
-
if(imagemap == null){
-
return 0;
-
}
-
else if(imagemap.containsKey(s)){
-
return imagemap.get(s);
-
}
-
else if(imagemap.containsKey(sEmpty)){
-
return imagemap.get(sEmpty);
-
}
-
else {
-
return 0;
-
}
-
}
-
-
private int refreshFileList()
-
{
-
-
File[] files = null;
-
try{
-
files = new File(path).listFiles();
-
}
-
catch(Exception e){
-
files = null;
-
}
-
if(files==null){
-
-
Toast.makeText(getContext(), sOnErrorMsg,Toast.LENGTH_SHORT).show();
-
return -1;
-
}
-
if(list != null){
-
list.clear();
-
}
-
else{
-
list = new ArrayList<Map<String, Object>>(files.length);
-
}
-
-
-
ArrayList<Map<String, Object>> lfolders = new ArrayList<Map<String, Object>>();
-
ArrayList<Map<String, Object>> lfiles = new ArrayList<Map<String, Object>>();
-
-
if(!this.path.equals(sRoot)){
-
-
Map<String, Object> map = new HashMap<String, Object>();
-
map.put("name", sRoot);
-
map.put("path", sRoot);
-
map.put("img", getImageId(sRoot));
-
list.add(map);
-
-
map = new HashMap<String, Object>();
-
map.put("name", sParent);
-
map.put("path", path);
-
map.put("img", getImageId(sParent));
-
list.add(map);
-
}
-
-
for(File file: files)
-
{
-
if(file.isDirectory() && file.listFiles()!=null){
-
-
Map<String, Object> map = new HashMap<String, Object>();
-
map.put("name", file.getName());
-
map.put("path", file.getPath());
-
map.put("img", getImageId(sFolder));
-
lfolders.add(map);
-
}
-
else if(file.isFile()){
-
-
String sf = getSuffix(file.getName()).toLowerCase();
-
if(suffix == null || suffix.length()==0 || (sf.length()>0 && suffix.indexOf("."+sf+";")>=0)){
-
Map<String, Object> map = new HashMap<String, Object>();
-
map.put("name", file.getName());
-
map.put("path", file.getPath());
-
map.put("img", getImageId(sf));
-
lfiles.add(map);
-
}
-
}
-
}
-
-
list.addAll(lfolders);
-
list.addAll(lfiles);
-
-
-
SimpleAdapter adapter = new SimpleAdapter(getContext(), list, R.layout.filedialogitem, new String[]{"img", "name", "path"}, new int[]{R.id.filedialogitem_img, R.id.filedialogitem_name, R.id.filedialogitem_path});
-
this.setAdapter(adapter);
-
return files.length;
-
}
-
@Override
-
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
-
-
String pt = (String) list.get(position).get("path");
-
String fn = (String) list.get(position).get("name");
-
if(fn.equals(sRoot) || fn.equals(sParent)){
-
-
File fl = new File(pt);
-
String ppt = fl.getParent();
-
if(ppt != null){
-
-
path = ppt;
-
}
-
else{
-
-
path = sRoot;
-
}
-
}
-
else{
-
File fl = new File(pt);
-
if(fl.isFile()){
-
-
((Activity)getContext()).dismissDialog(this.dialogid);
-
-
-
Bundle bundle = new Bundle();
-
bundle.putString("path", pt);
-
bundle.putString("name", fn);
-
-
this.callback.callback(bundle);
-
return;
-
}
-
else if(fl.isDirectory()){
-
-
-
path = pt;
-
}
-
}
-
this.refreshFileList();
-
}
-
}
-
}
下面是文件条目的一个布局(文件名:filedialogitem.xml):
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:id="@+id/vw1"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:background="#000000"
-
android:orientation="horizontal"
-
android:padding="4dp" >
-
-
-
<ImageView
-
android:id="@+id/filedialogitem_img"
-
android:layout_width="32dp"
-
android:layout_height="32dp"
-
android:layout_margin="4dp"/>
-
-
-
-
<LinearLayout
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:orientation="vertical" >
-
-
-
-
-
-
<TextView
-
android:id="@+id/filedialogitem_name"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:textColor="#FFFFFF"
-
android:textSize="18sp"
-
android:textStyle="bold" />
-
-
-
<TextView
-
android:id="@+id/filedialogitem_path"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:paddingLeft="10dp"
-
android:textColor="#FFFFFF"
-
android:textSize="14sp" />
-
-
</LinearLayout>
-
-
</LinearLayout>
下面是使用的例子:
-
-
package com.example.openfiledemo;
-
-
import java.util.HashMap;
-
import java.util.Map;
-
-
import android.os.Bundle;
-
import android.app.Activity;
-
import android.app.Dialog;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
-
public class OpenFileDemo extends Activity {
-
-
static private int openfileDialogId = 0;
-
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_open_file_demo);
-
-
-
-
findViewById(R.id.button_openfile).setOnClickListener(new OnClickListener() {
-
@Override
-
public void onClick(View arg0) {
-
showDialog(openfileDialogId);
-
}
-
});
-
}
-
-
@Override
-
protected Dialog onCreateDialog(int id) {
-
if(id==openfileDialogId){
-
Map<String, Integer> images = new HashMap<String, Integer>();
-
-
images.put(OpenFileDialog.sRoot, R.drawable.filedialog_root);
-
images.put(OpenFileDialog.sParent, R.drawable.filedialog_folder_up);
-
images.put(OpenFileDialog.sFolder, R.drawable.filedialog_folder);
-
images.put("wav", R.drawable.filedialog_wavfile);
-
images.put(OpenFileDialog.sEmpty, R.drawable.filedialog_root);
-
Dialog dialog = OpenFileDialog.createDialog(id, this, "打开文件", new CallbackBundle() {
-
@Override
-
public void callback(Bundle bundle) {
-
String filepath = bundle.getString("path");
-
setTitle(filepath);
-
}
-
},
-
".wav;",
-
images);
-
return dialog;
-
}
-
return null;
Android开发 打开文件 选择文件对话框,布布扣,bubuko.com
Android开发 打开文件 选择文件对话框
原文:http://blog.csdn.net/aigoogle/article/details/36187831