话不多少,先上图一览:
接下来我们开始写个app测试:
1、新建Android工程:FileSelectorTest
2、更改MainActivity:
在里面写四个textview模拟button,分别是文件单选,文件多选,文件夹路径单选,文件夹路径多选,然后重写textview点击事件进入FsActivity,传入参数不同,选择模式不同。
复写onActivityResult,用来获取从文件/文件夹选择器传递回来的数据。
package cn.darkranger.fileselectortest; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView; import android.widget.Toast; import dr.android.fileselector.FileSelectConstant; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private static final Integer FILE_SELECTOR_REQUEST_CODE = 2016; private TextView mFileTv; private TextView mFileMultiTv; private TextView mFolderTv; private TextView mFolderMultiTv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); initEvent(); } private void initView() { mFileTv = (TextView) findViewById(R.id.id_mode_file); mFileMultiTv = (TextView) findViewById(R.id.id_mode_file_multi); mFolderTv = (TextView) findViewById(R.id.id_mode_folder); mFolderMultiTv = (TextView) findViewById(R.id.id_mode_folder_multi); } private void initData() { } private void initEvent() { // 文件单选 mFileTv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(getApplicationContext(), FsActivity.class); intent.putExtra(FileSelectConstant.SELECTOR_REQUEST_CODE_KEY, FileSelectConstant.SELECTOR_MODE_FILE); startActivityForResult(intent, FILE_SELECTOR_REQUEST_CODE); } }); // 文件多选 mFileMultiTv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(getApplicationContext(), FsActivity.class); intent.putExtra(FileSelectConstant.SELECTOR_REQUEST_CODE_KEY, FileSelectConstant.SELECTOR_MODE_FILE); intent.putExtra(FileSelectConstant.SELECTOR_IS_MULTIPLE, true); startActivityForResult(intent, FILE_SELECTOR_REQUEST_CODE); } }); // 文件夹单选 mFolderTv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(getApplicationContext(), FsActivity.class); intent.putExtra(FileSelectConstant.SELECTOR_REQUEST_CODE_KEY, FileSelectConstant.SELECTOR_MODE_FOLDER); startActivityForResult(intent, FILE_SELECTOR_REQUEST_CODE); } }); // 文件夹多选 mFolderMultiTv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(getApplicationContext(), FsActivity.class); intent.putExtra(FileSelectConstant.SELECTOR_REQUEST_CODE_KEY, FileSelectConstant.SELECTOR_MODE_FOLDER); intent.putExtra(FileSelectConstant.SELECTOR_IS_MULTIPLE, true); startActivityForResult(intent, FILE_SELECTOR_REQUEST_CODE); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent bundle) { super.onActivityResult(requestCode, resultCode, bundle); Log.i(TAG, "requestCode: " + requestCode + "; resultCode: " + resultCode); if (resultCode == RESULT_OK) { Toast.makeText(this, "paths: " + bundle.getStringArrayListExtra(FileSelectConstant.SELECTOR_BUNDLE_PATHS), Toast.LENGTH_SHORT).show(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
3、新建FsActivity
(1)添加依赖包DR-lib-fileselector:
(2)继承FileSelectActivity
(3)onCreate、onCreateOptionsMenu和onOptionsItemSelected直接使用FileSelectActivity的方法,super.xxx();
(4)复写FileSelectActivity的方法initParams,里面设置了一些基本的信息,包括选择文件的标题,选择文件夹路径的标题,文件夹的图标,文件的图标,图标宽度和高度,当然不写的话都使用默认值。
(5)还可以复写onBackPressed()和onClickOkBtn(),分别是下面虚拟返回键的操作和右上角选择完成后的操作。当然不写的话也都是用默认FileSelectActivity的默认操作
package cn.darkranger.fileselectortest; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import dr.android.fileselector.FileSelectActivity; public class FsActivity extends FileSelectActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public boolean onCreateOptionsMenu(Menu menu) { return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { return super.onOptionsItemSelected(item); } @Override public void initParams() { // 这些参数都有默认值,不设置也是可以的 // setSelectorFileTitle("this is file title"); // setSelectorFolderTitle("this is folder title"); // setSelectorFileIcon(R.drawable.ic_fileselect_file); // setSelectorFolderIcon(R.drawable.ic_fileselect_folder); // setSelectorIconHeight(150); // setSelectorIconWidth(150); } } @Override public void onBackPressed() { super.onBackPressed(); } @Override public void onClickOkBtn() { super.onClickOkBtn(); } }
4、在AndroidManifest.xml中添加读写存储的权限
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.darkranger.fileselectortest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="22" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="cn.darkranger.fileselectortest.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="cn.darkranger.fileselectortest.FsActivity" android:label="@string/title_activity_fs" > </activity> </application> </manifest>
5、完成配置,并运行。
项目源码:http://files.cnblogs.com/files/wrcold520/FileSelectorTest.zip(lib包里面去掉了v4包,依赖于DR-lib-fileselector)
依赖包:http://files.cnblogs.com/files/wrcold520/DR-lib-fileselector.zip(lib包里面去掉了v4包,DR-lib-fileselector依赖包依赖于v7包,如下图,v7的依赖包里面有v4包)