Android 简易文件选择Dialog
16lz
2021-01-23
fileselect.java
package com.example.fileselect;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class fileselect extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.fileselect);Button btn = (Button)this.findViewById(R.id.button1);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {show();}});}private void show(){fileselectDialog dts = new fileselectDialog(this);dts.show();}}
package com.example.fileselect;import java.io.File;import java.util.ArrayList;import java.util.HashMap;import android.annotation.SuppressLint;import android.app.Dialog;import android.content.Context;import android.os.Bundle;import android.os.Environment;import android.os.Handler;import android.os.Message;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.BaseAdapter;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.ListView;import android.widget.TextView;public class fileselectDialog extends Dialog {private ArrayList> CurrentDirOrFileList;private Handler MainHandler;private mAdapter myAdapter;private String currentPath;private Context mContext;private TextView currentpathTextView;public fileselectDialog(Context context) {super(context);mContext = context;CurrentDirOrFileList = new ArrayList>();myAdapter = new mAdapter();currentPath = Environment.getExternalStorageDirectory().getAbsolutePath();}class mAdapter extends BaseAdapter {@Overridepublic View getView(int position, View convertView, ViewGroup parent) {convertView = LayoutInflater.from(mContext).inflate(R.layout.sub,null);ImageView iv = (ImageView)convertView.findViewById(R.id.imageView1);TextView tv = (TextView)convertView.findViewById(R.id.textView1);HashMap File = CurrentDirOrFileList.get(position);if(File.get("type").equals("D"))iv.setImageResource(R.drawable.dir);if(File.get("type").equals("F"))iv.setImageResource(R.drawable.file);tv.setText(File.get("name"));return convertView;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic Object getItem(int position) {return null;}@Overridepublic int getCount() {return CurrentDirOrFileList.size();}}@SuppressLint("HandlerLeak")@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.fileselectdialog);currentpathTextView = (TextView)findViewById(R.id.textView1);ListView lv = (ListView)findViewById(R.id.listView1);lv.setAdapter(myAdapter);lv.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {String path = CurrentDirOrFileList.get(arg2).get("path");File f = new File(path);if(f.isDirectory()){currentPath = path;getFiles();}}});ImageButton BackBtn = (ImageButton)findViewById(R.id.imageButton1);BackBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {File f = new File(currentPath);String parentpath = f.getParentFile().getAbsolutePath();currentPath = parentpath;getFiles();}});MainHandler = new Handler(){@Overridepublic void handleMessage(Message msg){if(msg.obj.toString().equals("find")){findTheard ft = new findTheard(currentPath);ft.start();}if(msg.obj.toString().equals("refresh")){currentpathTextView.setText(currentPath);myAdapter.notifyDataSetChanged();}}};getFiles();}public void dialogdismiss(){this.dismiss();}private class findTheard extends Thread {private String findPath;private findTheard(String Path){findPath = Path;}@Overridepublic void run(){File f = new File(findPath);if(!f.isDirectory()){return;}CurrentDirOrFileList.clear();File[] flist = f.listFiles();if(flist != null){for(File subfile : flist){HashMap filemap = new HashMap();if(subfile.isDirectory())filemap.put("type", "D");if(subfile.isFile())filemap.put("type", "F");String filename = subfile.getName();filemap.put("name", filename);String filepath = subfile.getAbsolutePath();filemap.put("path", filepath);CurrentDirOrFileList.add(filemap);}}Message msg = MainHandler.obtainMessage();msg.obj = "refresh";MainHandler.sendMessage(msg);}}private void getFiles(){Message msg = MainHandler.obtainMessage();msg.obj = "find";MainHandler.sendMessage(msg);}}
fileselect.xml
<?xml version="1.0" encoding="utf-8"?>
fiileselectdialog.xml
<?xml version="1.0" encoding="utf-8"?>
sub.xml
<?xml version="1.0" encoding="utf-8"?>
更多相关文章
- android文件操作工具类
- 在Android中创建文件
- Android 删除指定文件代码
- Android中XML文件的使用
- Android使用HttpURLConnection上传文件
- Android so 文件简介
- Android 文件工具FileUtil
- android 在SD卡创建文件夹
- Android下各文件方法获取的路径