How to implement file picker android 11
Asked Answered
D

2

1

Can anyone please share complete solution for File Picker in Android 11 for both Internal and External Storage

Dirty answered 20/9, 2021 at 6:17 Comment(1)
try this : github.com/anggrayudi/SimpleStorageUnable
P
0
**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"
Peroneus answered 20/9, 2021 at 6:43 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Zurn
F
0

Here Code For Android 11 File Upload using Intent and also you dont have to mention MANAGE_EXTERNAL_STORAGE Permission in AndroidManifest.xml file i have implemented without this permission.

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

here is onActivityResult Code Pick File Name and other information

    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();
    
                       
                }
Feverroot answered 7/2, 2022 at 11:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.