我如何使用FilteredFilePickerFragment为nonnonsense - filepicker



我必须创建一个文件选择器,它将使我能够选择特定类型的文件,如"pdf", "ppt", "odt"等等。为此,我创建了一个FilteredFilePickerFragment,如下所示。现在我需要使用这个片段,但我不知道如何使用。

这是我的意图在MainActivity:

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);
    }
}

基本上你使用Intent的activity应该Intent给你一个新的activity (URL:https://github.com/spacecowboy/NoNonsense-FilePicker/blob/master/sample/src/main/java/com/nononsenseapps/filepicker/sample/ImagePickerActivity.java)

上面的intent创建了一个fragment调用AbstractFilePickerFragment fragment = new ImagePickerFragment();

图片选取器片段(https://github.com/spacecowboy/NoNonsense-FilePicker/blob/master/sample/src/main/java/com/nononsenseapps/filepicker/sample/ImagePickerFragment.java),它是一个类,扩展文件选取器片段

这就是只显示某些扩展文件(可能是。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;
}
}

ImageFilePickerFragment类似于你的FilteredFilePickerFragment类。

public class ImageFilePickerFragment extends FilePickerFragment {
/**
 *
 * @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) {
    boolean ret = super.isItemVisible(file);
    if (ret && !isDir(file) && (mode == MODE_FILE || mode == MODE_FILE_AND_DIR)) {
        String ext = getExtension(file);
        return ext != null && (".jpg".equalsIgnoreCase(ext)||".jpeg".equalsIgnoreCase(ext)||".png".equalsIgnoreCase(ext));
    }
    return ret;
}
}

运行活动的代码。

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);

相关内容

  • 没有找到相关文章

最新更新