首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何实现文件选取器android 11

如何实现文件选取器android 11
EN

Stack Overflow用户
提问于 2021-09-20 06:18:00
回答 3查看 1.2K关注 0票数 0

有没有人可以分享一下Android 11内部和外部存储的文件拾取器的完整解决方案

EN

回答 3

Stack Overflow用户

发布于 2021-09-20 06:36:38

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

适用于单文件的

代码语言:javascript
复制
// 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打开目录

代码语言:javascript
复制
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,像这样的

代码语言:javascript
复制
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.
        }
    }
}
票数 0
EN

Stack Overflow用户

发布于 2021-09-20 06:43:59

代码语言:javascript
复制
**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"
票数 0
EN

Stack Overflow用户

发布于 2022-02-07 11:27:33

这里的代码为安卓11文件上传使用意图,而且你也不必提及AndroidManifest.xml文件中的MANAGE_EXTERNAL_STORAGE权限,我已经实现了没有这个权限。

代码语言:javascript
复制
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代码选择文件名和其他信息

代码语言:javascript
复制
    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();
    
                       
                }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69249858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档