【问题标题】:How to implement file picker android 11如何实现文件选择器android 11
【发布时间】:2021-09-20 06:17:59
【问题描述】:

任何人都可以分享 Android 11 中用于内部和外部存储的文件选择器的完整解决方案

【问题讨论】:

标签: android android-11 filepicker


【解决方案1】:

您可以使用 SAF(存储访问框架)选择文件

对于单个文件

// Request code for selecting a PDF document.
const val PICK_PDF_FILE = 2

fun openFile(pickerInitialUri: Uri) {
    val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
        addCategory(Intent.CATEGORY_OPENABLE)
        type = "application/pdf"

您也可以使用 SAF 打开目录

fun openDirectory(pickerInitialUri: Uri) {
    // Choose a directory using the system's file picker.
    val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
        // Optionally, specify a URI for the directory that should be opened in
        // the system file picker when it loads.
        putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri)
    }

    startActivityForResult(intent, your-request-code)
}
    
            // Optionally, specify a URI for the file that should appear in the
            // system file picker when it loads.
            putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri)
        }
    
        startActivityForResult(intent, PICK_PDF_FILE)
    }

像这样获取文件的 URI

override fun onActivityResult(
        requestCode: Int, resultCode: Int, resultData: Intent?) {
    if (requestCode == your-request-code
            && resultCode == Activity.RESULT_OK) {
        // The result data contains a URI for the document or directory that
        // the user selected.
        resultData?.data?.also { uri ->
            // Perform operations on the document using its URI.
        }
    }
}

【讨论】:

    【解决方案2】:
    **For Single File**
    // Request code for selecting a PDF document.
    const val PICK_PDF_FILE = 2
    
    fun openFile(pickerInitialUri: Uri) {
        val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
            addCategory(Intent.CATEGORY_OPENABLE)
            type = "application/pdf"
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。
    【解决方案3】:

    这里是使用 Intent 上传 Android 11 文件的代码,而且您不必在 AndroidManifest.xml 文件中提及 MANAGE_EXTERNAL_STORAGE 权限,我在没有此权限的情况下实现了。

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                        Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
                        chooseFile.setType("application/pdf");
                        chooseFile = Intent.createChooser(chooseFile, "Choose a file");
                        startActivityForResult(chooseFile, Constants.REQUEST_PICK_FILE);
                    }else{
    String[] types = new String[]{"pdf"};
        FilePickerBuilder.getInstance().setMaxCount(UPLOAD_LIMIT - attachmentModelList.size())
                .setActivityTheme(R.style.AppTheme)
                .setActivityTitle(mActivity.getString(R.string.select_documents))
                /*.sortDocumentsBy(SortingTypes.NAME)*/
                .enableDocSupport(false)
                .addFileSupport("PDF", types)
                /*.setSelectedFiles(masterList !!)*/
                .pickFile(this);
    }
    

    这里是onActivityResult Code Pick File Name等信息

        if (requestCode == Constants.REQUEST_PICK_FILE &&
                            resultCode == Activity.RESULT_OK) {
        
                        Uri uri = data.getData();
                        String mimeType = getActivity().getContentResolver().getType(uri);
                        String filename;
                        if (mimeType == null) {
                            String path = CommonMethod.getPath(getActivity(), uri);
        //                    if (path == null) {
        //                        filename = FilenameUtils.getName(uri.toString());
        //                    } else {
                            File file = new File(path);
                            filename = file.getName();
        //                    }
                        } else {
                            Uri returnUri = data.getData();
                            Cursor returnCursor = getActivity().getContentResolver().query(returnUri, null, null, null, null);
                            int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                            int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
                            returnCursor.moveToFirst();
                            filename = returnCursor.getString(nameIndex);
                            String size = Long.toString(returnCursor.getLong(sizeIndex));
                        }
                        File fileSave = getActivity().getExternalFilesDir(null);
                        String sourcePath = getActivity().getExternalFilesDir(null).toString();
                        File targetFile = null;
                        try {
                            targetFile = new File(sourcePath + "/" + filename);
                            boolean success = CommonMethod.copyFileStream(new File(sourcePath + "/" + filename), uri, getActivity());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if (targetFile != null) {
                            String fileName = Utils.getFileName(getActivity(),Uri.fromFile(targetFile));
     String filePath = targetFile.getPath();
        
                           
                    }
    

    【讨论】:

      猜你喜欢
      • 2013-11-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-25
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多