【问题标题】:How to use intent for choosing file browser to select file如何使用意图选择文件浏览器来选择文件
【发布时间】:2012-03-29 10:42:33
【问题描述】:

如何使用intent提示用户选择“完成操作”来选择应用程序选择文件(假设设备中有几个应用程序浏览文件)

我想使用扩展名过滤文件..(例如:*.sav、*.props)

提前谢谢你

【问题讨论】:

标签: android android-intent


【解决方案1】:

你可以这样使用:

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

但是我真的怀疑你可以设置一个过滤器第三方文件浏览器。 或者您可以尝试使用此文件对话框:http://code.google.com/p/android-file-dialog/

【讨论】:

  • 感谢您的链接.. :) 它非常简单实用的文件浏览器
  • 我需要改用intent.setType("*/*")。我知道没有以file/ 开头的 MIME 类型。
【解决方案2】:

这将打开内置文件资源管理器(如果可用),否则将要求您选择已安装的文件资源管理器。

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

【讨论】:

  • 如何在 onActivityResult 中获取该文件?
【解决方案3】:

它可以帮助您选择一个 doc 文件,您可以根据需要更改操作

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

    【讨论】:

    • if/else 中的代码也是如此
    猜你喜欢
    • 1970-01-01
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-13
    相关资源
    最近更新 更多