How to use intent for choosing file browser to sel

2019-01-06 20:56发布

问题:

How to use intent to prompt user to choose "finish action with" to choosing app to select file (assuming there is a few app to browse file in the device)

I want to filter the file using an extension.. (eg. : *.sav, *.props)

Thank you in advance

回答1:

You can use something like this:

    ....  
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("file/*");
    startActivityForResult(intent, YOUR_RESULT_CODE);
    .... 

But I realy doubt that you can set a filter third-party file browsers. Or you can try to use this file dialog: http://code.google.com/p/android-file-dialog/



回答2:

this will open the build-in file explorer if available, otherwise will ask u to select a file explorer that u have installed.

private void showFileChooser() {

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    //intent.setType("*/*");      //all files
    intent.setType("text/xml");   //XML file only
    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(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show();
    }
}


回答3:

It may help you to choose a doc file and u may change action as per your need

        Intent intent;
        if (VERSION.SDK_INT >= 19) {
            intent = new Intent("android.intent.action.OPEN_DOCUMENT");
            intent.setType("*/*");
        } else {
            PackageManager packageManager =getActivity().getPackageManager();
            intent = new Intent("android.intent.action.GET_CONTENT");
            intent.setType("file*//*");
            if (packageManager.queryIntentActivities(intent,MEDIA_TYPE_IMAGE).size() == 0) {
                UserToast.show(getActivity(), getResources().getString(R.string.no_file_manager_present));
            }
        }
        if (getActivity().getPackageManager().resolveActivity(intent, NativeProtocol.MESSAGE_GET_ACCESS_TOKEN_REQUEST) != null) {
            startActivityForResult(intent, UPLOAD_FILE);
        }


回答4:

// check here to KITKAT or new version and this will solve the samsung file explore issue too.

boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
if (isKitKat) {
    Intent intent = new Intent(); 
    intent.setType("*/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(intent,FILE_SELECT_CODE);
} else {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
    startActivityForResult(intent,FILE_SELECT_CODE);
}