0

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

2 に答える 2

2

基本的に、インテントを使用しているアクティビティは、新しいアクティビティを意図する必要があります (URL: https://github.com/spacecowboy/NoNonsense-FilePicker/blob/master/sample/src/main/java/com/nononsenseapps/filepicker/ sample/ImagePickerActivity.java )

上記のインテントは、フラグメント呼び出しを作成します。 AbstractFilePickerFragment fragment = new ImagePickerFragment();

File Picker を拡張するクラスであるImaqe Picker Fragment ( https://github.com/spacecowboy/NoNonsense-FilePicker/blob/master/sample/src/main/java/com/nononsenseapps/filepicker/sample/ImagePickerFragment.java )断片

特定の拡張子ファイル (.doc、.docx、.pdf など) のみを表示するために必要なのはこれだけです。

于 2015-09-28T03:48:17.663 に答える
2

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);
于 2017-01-06T00:09:25.293 に答える