NoNonsense-FilePicker文件选择器
介绍:
一个风格简洁的可扩展文件选择器。 如果你想要这样一个文件选择器:1.容易扩展,文件来源既可以是本地sdcard,也可以是来自云端的dropboxapi。 2.可以在选择器中创建目录。本项目具备上述的两个要求,同时很好的适配了平板和手机两种UI效果。项目的核心是在一个abstract 类中,因此你可以很方便的继承以实现自己需要的选择器。
运行效果:
使用说明:
首选需要加上文件访问权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
将选择器的app注册到AndroidManifest.xml
<activity
android:name="com.nononsenseapps.filepicker.FilePickerActivity"
android:label="@string/app_name"
android:theme="@style/FilePicker.Theme">
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
在java代码中调用选择器:
// This always works
Intent i = new Intent(context, FilePickerActivity.class);
// This works if you defined the intent filter
// Intent i = new Intent(Intent.ACTION_GET_CONTENT);
// Set these depending on your use case. These are the defaults.
i.putExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false);
i.putExtra(FilePickerActivity.EXTRA_ALLOW_CREATE_DIR, false);
i.putExtra(FilePickerActivity.EXTRA_MODE, FilePickerActivity.MODE_FILE);
startActivityForResult(i, FILE_CODE);
获取选择的返回值:
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FILE_CODE && resultCode == Activity.RESULT_OK) {
if (data.getBooleanExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false)) {
// For JellyBean and above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
ClipData clip = data.getClipData();
if (clip != null) {
for (int i = 0; i < clip.getItemCount(); i++) {
Uri uri = clip.getItemAt(i).getUri();
// Do something with the URI
}
}
// For Ice Cream Sandwich
} else {
ArrayList<String> paths = data.getStringArrayListExtra
(FilePickerActivity.EXTRA_PATHS);
if (paths != null) {
for (String path: paths) {
Uri uri = Uri.parse(path);
// Do something with the URI
}
}
}
} else {
Uri uri = data.getData();
// Do something with the URI
}
}
}
已下载
0