Android 如何为非OnSense文件选择器使用FilteredFilePickerFragment?
我必须创建一个文件选择器,使我能够选择特定类型的文件,如“pdf”、“ppt”、“odt”等。为此,我创建了一个filteredflepickerfragment,如下所示。现在我需要使用这个片段,但我不知道如何使用 以下是我在主要活动中的意图:Android 如何为非OnSense文件选择器使用FilteredFilePickerFragment?,android,android-fragments,android-library,filepicker,Android,Android Fragments,Android Library,Filepicker,我必须创建一个文件选择器,使我能够选择特定类型的文件,如“pdf”、“ppt”、“odt”等。为此,我创建了一个filteredflepickerfragment,如下所示。现在我需要使用这个片段,但我不知道如何使用 以下是我在主要活动中的意图: Intent i = new Intent(Intent.ACTION_GET_CONTENT); // Set these depending on your use case. These are the defaults. i.
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);
// Configure initial directory by specifying a String.
// You could specify a String like "/storage/emulated/0/", but that can
// dangerous. Always use Android's API calls to get paths to the SD-card or
// internal memory.
i.putExtra(FilePickerActivity.EXTRA_START_PATH, Environment.getExternalStorageDirectory().getPath());
startActivityForResult(i, FILE_CODE);
这是我的过滤片段
import android.support.annotation.NonNull;
import com.nononsenseapps.filepicker.FilePickerFragment;
import java.io.File;
public class FilteredFilePickerFragment extends FilePickerFragment {
// File extension to filter on
private static final String EXTENSION = ".pdf";
/**
*
* @param file
* @return The file extension. If file has no extension, it returns null.
*/
private String getExtension(@NonNull File file) {
String path = file.getPath();
int i = path.lastIndexOf(".");
if (i < 0) {
return null;
} else {
return path.substring(i);
}
}
@Override
protected boolean isItemVisible(final File file) {
if (mode == MODE_FILE || mode == MODE_FILE_AND_DIR) {
return EXTENSION.equalsIgnoreCase(getExtension(file));
}
return isDir(file);
}
}
import android.support.annotation.NonNull;
导入com.nononsenseapps.filepicker.FilePickerFragment;
导入java.io.File;
公共类FilteredFilePickerFragment扩展了FilePickerFragment{
//要筛选的文件扩展名
私有静态最终字符串扩展名=“.pdf”;
/**
*
*@param文件
*@返回文件扩展名。如果文件没有扩展名,则返回null。
*/
私有字符串getExtension(@NonNull文件){
字符串路径=file.getPath();
int i=path.lastIndexOf(“.”);
if(i<0){
返回null;
}否则{
返回路径子串(i);
}
}
@凌驾
受保护的布尔值isItemVisible(最终文件){
if(mode==mode_FILE | | mode==mode_FILE_和_DIR){
返回扩展名.equalsIgnoreCase(getExtension(file));
}
返回isDir(文件);
}
}
基本上,您正在使用Intent的活动应该是一个新活动(URL:)
上述意图创建了一个片段调用
AbstractFilePickerFragment=新的ImagePickerFragment()
Imaqe选择器片段
()这是一个扩展文件选择器片段的类
这就是只显示某些扩展文件(可能是.doc、.docx、.pdf)所需的全部内容。基本上,您正在使用Intent的活动应该显示一个新的活动(URL:) 上述意图创建了一个片段调用 AbstractFilePickerFragment=新的ImagePickerFragment() Imaqe选择器片段 ()这是一个扩展文件选择器片段的类
这就是只显示某些扩展文件(可能是.doc、.docx、.pdf)所需的全部内容。如果您检查FilePickerActivity类的源代码,您可能会发现如何实现该片段 扩展FilePickerActivity类的自定义类
@SuppressLint("Registered")
public class ImageFilePickerActivity extends AbstractFilePickerActivity<File> {
public ImageFilePickerActivity() {
super();
}
@Override
protected AbstractFilePickerFragment<File> getFragment(
@Nullable final String startPath, final int mode, final boolean allowMultiple,
final boolean allowCreateDir, final boolean allowExistingFile,
final boolean singleClick) {
AbstractFilePickerFragment<File> fragment = new ImageFilePickerFragment();
// startPath is allowed to be null. In that case, default folder should be SD-card and not "/"
fragment.setArgs(startPath != null ? startPath : Environment.getExternalStorageDirectory().getPath(),
mode, allowMultiple, allowCreateDir, allowExistingFile, singleClick);
return fragment;
}
}
如果您检查FilePickerActivity类的源代码,您可能会发现如何实现该片段 扩展FilePickerActivity类的自定义类
@SuppressLint("Registered")
public class ImageFilePickerActivity extends AbstractFilePickerActivity<File> {
public ImageFilePickerActivity() {
super();
}
@Override
protected AbstractFilePickerFragment<File> getFragment(
@Nullable final String startPath, final int mode, final boolean allowMultiple,
final boolean allowCreateDir, final boolean allowExistingFile,
final boolean singleClick) {
AbstractFilePickerFragment<File> fragment = new ImageFilePickerFragment();
// startPath is allowed to be null. In that case, default folder should be SD-card and not "/"
fragment.setArgs(startPath != null ? startPath : Environment.getExternalStorageDirectory().getPath(),
mode, allowMultiple, allowCreateDir, allowExistingFile, singleClick);
return fragment;
}
}
Intent i = new Intent(getBaseContext(), ImageFilePickerActivity.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(ImageFilePickerActivity.EXTRA_ALLOW_MULTIPLE, false);
i.putExtra(ImageFilePickerActivity.EXTRA_ALLOW_CREATE_DIR, false);
i.putExtra(ImageFilePickerActivity.EXTRA_MODE, ImageFilePickerActivity.MODE_FILE);
// Configure initial directory by specifying a String.
// You could specify a String like "/storage/emulated/0/", but that can
// dangerous. Always use Android's API calls to get paths to the SD-card or
// internal memory.
i.putExtra(ImageFilePickerActivity.EXTRA_START_PATH, Environment.getExternalStorageDirectory().getPath());
startActivityForResult(i, RESULT_LOAD_IMAGE);