FileExplorerActivity 选择器
package chg.com.fileselect;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by g on 2016/9/8.
*/
public class FileExplorerActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
ListView listView;
SimpleAdapter adapter;
String rootPath = Environment.getExternalStorageDirectory().getPath();
String currentPath = rootPath;
List<Map<String, Object>> list = new ArrayList<>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_explorer);
listView = (ListView) findViewById(R.id.list_view);
adapter = new SimpleAdapter(this, list, R.layout.list_item,
new String[]{"name", "img"}, new int[]{R.id.name, R.id.img});
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
refreshListItems(currentPath);
}
private void refreshListItems(String path) {
setTitle(path);
File[] files = new File(path).listFiles();
list.clear();
if (files != null) {
for (File file : files) {
Map<String, Object> map = new HashMap<>();
if (file.isDirectory()) {
map.put("img", R.mipmap.ic_launcher);
} else {
map.put("img", R.mipmap.ic_launcher);
}
map.put("name", file.getName());
map.put("currentPath", file.getPath());
list.add(map);
}
}
adapter.notifyDataSetChanged();
}
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
currentPath = (String) list.get(position).get("currentPath");
File file = new File(currentPath);
if (file.isDirectory())
refreshListItems(currentPath);
else {
Intent intent = new Intent();
intent.putExtra("apk_path", file.getPath());
setResult(RESULT_OK, intent);
finish();
}
}
@Override
public void onBackPressed() {
if (rootPath.equals(currentPath)) {
super.onBackPressed();
} else {
File file = new File(currentPath);
currentPath = file.getParentFile().getPath();
refreshListItems(currentPath);
}
}
}
选择器layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
listviewItem
<?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="wrap_content"
android:padding="4dp"
android:orientation="horizontal">
<ImageView android:id="@+id/img"
android:layout_width="32dp"
android:layout_margin="4dp"
android:layout_gravity="center_vertical"
android:layout_height="32dp"/>
<TextView android:id="@+id/name"
android:textSize="18sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:layout_height="50dp"/>
</LinearLayout>
MainActivity
package chg.com.fileselect;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView path;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
path = (TextView) findViewById(R.id.path);
}
public void select(View view) {
Intent intent = new Intent(MainActivity.this, FileExplorerActivity.class);
startActivityForResult(intent,1001);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(1001==requestCode&&RESULT_OK==resultCode){
String apk_path = data.getStringExtra("apk_path");
path.setText(apk_path);
}
}
}
MainActivity 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="chg.com.fileselect.MainActivity">
<Button
android:onClick="select"
android:text="选择文件"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/path"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>