1

Not able to pick a document from this specific option. Please have look I use the below code for it. I have created one class that processes and gives me a file path.Everythings works fine if I select from image, audio, video and download but not works as I select media from document

 XXPermissions.with(this)
                    .permission(Permission.MANAGE_EXTERNAL_STORAGE)
                    .permission(Permission.CAMERA)
                    .request(new OnPermissionCallback() {
                        @Override
                        public void onGranted(List<String> permissions, boolean all) {
                            Intent chooseFile = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                            chooseFile.addCategory(Intent.CATEGORY_OPENABLE);
                            chooseFile.setType("*/*");
                            chooseFile.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
                            chooseFile = Intent.createChooser(chooseFile, "Choose a file");
                            startActivityForResult(chooseFile, 1001);
                        }
                    });


 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case 1001:
            if (resultCode == RESULT_OK) {
                Log.e("FILE", "" + data.getData());
                Uri uri = data.getData();
                String uriString = uri.toString();
                File myFile = new File(uriString);
                File file = FileUtils.getFile(activity, uri);
                String displayName = null;

                if (uriString.startsWith("content://")) {
                    Cursor cursor = null;
                    try {
                        cursor = getContentResolver().query(uri, null, null, null, null);
                        if (cursor != null && cursor.moveToFirst()) {
                            displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                        }
                    } finally {
                        cursor.close();
                    }
                } else if (uriString.startsWith("file://")) {
                    displayName = myFile.getName();
                }

                uploadFiletoAWS(file.getPath(), ext);
            }
            break;
    }

    super.onActivityResult(requestCode, resultCode, data);
}

if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];
            Log.e("TYPE", "" + type);
            Uri contentUri;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            } else {
                contentUri = MediaStore.getDocumentUri(context, uri);
            }
            selection = "_id=?";
            selectionArgs = new String[]{split[1]};
            return getDataColumn(context, contentUri, selection, selectionArgs);
        }

enter image description here

Amit Patel
  • 11
  • 2
  • Please start with the used intent. Then post onActivityResult code. Then show how you treat the obtained uri. Before that tell where you are after and what your code should do. – blackapps Jun 14 '21 at 11:05
  • Please check now – Amit Patel Jun 14 '21 at 11:12
  • `Not able to pic document when i select document from file picker on latest android 11` The subject makes no sense. The user is able to pick any document and as soon as onActivityResult is triggeted your document is data.getData(). You did not tell where you are after or what the real problem is. – blackapps Jun 14 '21 at 11:17
  • My real problem is mention on image – Amit Patel Jun 14 '21 at 11:21
  • You have to tell your problem. And tell what you want. We will not look at images at fore hand. – blackapps Jun 14 '21 at 11:24
  • From the file picker, if I select any file from the image, audio and video then it works fine but if I choose file from the document option then it will not return anything on MediaStore this is the problem. – Amit Patel Jun 14 '21 at 11:29
  • @blackapps He is implying literal "Documents" when the file picker opens. Under it we get a section for Images, Video etc. But there is a section for literal "Documents" and whenever something is being picked from that, it does not return anything. He attached the image but since the account is new it is not showing up directly. – che10 Jun 14 '21 at 11:34
  • `whenever something is being picked from that, it does not return anything` It will return a nice uri too. @che10 – blackapps Jun 14 '21 at 11:41

0 Answers0