在android中添加过滤器到File Picker

时间:2017-03-15 01:25:41

标签: android

我从文件管理器中选择一个文件,现在我只想选择(.rar,.zip,.apk),其余的都是禁用的。这是我的代码:

private void showFileChooser() {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    try {
        startActivityForResult(
                Intent.createChooser(intent, "Select a File to Upload"),
                FILE_SELECT_CODE);
    } catch (android.content.ActivityNotFoundException ex) {
        // Potentially direct the user to the Market with a Dialog
        Toast.makeText(getActivity(), "Please install a File Manager.",
                Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case FILE_SELECT_CODE:
            if (resultCode == RESULT_OK) {



                Uri selectedFileUri = data.getData();
                selectedFilePath = FilePath.getPath(getActivity(),selectedFileUri);
                Log.i(TAG,"Selected File Path:" + selectedFilePath);


                bundleData.putString("appFile", selectedFilePath);
                btnAppFile.setText(getFileName(selectedFileUri));
            }
            break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

如何在此代码中添加类型过滤器?

1 个答案:

答案 0 :(得分:0)

你试过这个吗?

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/zip|application/x-rar-compressed");
startActivityForResult(intent, YOUR_REQUEST_CODE);