1
TYPE.DOCUMENT -> {
                val uri = MediaStore.Files.getContentUri("external")
                val selection = MediaStore.Files.FileColumns.DATA + " like '%/$folderName/%' AND " + MediaStore.Files.FileColumns.MIME_TYPE + " IN (" + fileExtensions.joinToString(",") { "'application/$it'" } + ")"
                val cursor = contentResolver.query(uri, null, selection, null, null)

                if (cursor == null) {
                    showErrorMessage("Failed to retrieve documents: cursor is null.")
                } else {
                    while (cursor.moveToNext()) {
                        val docPath = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA))
                        docList.add(docPath)
                    }
                    cursor.close()
                }
            }

my code working till android 12

avariant
  • 2,234
  • 5
  • 25
  • 33
  • DATA column was Deprecated in API level 29 https://developer.android.com/reference/android/provider/MediaStore.MediaColumns#DATA don't try an use it as it – Andrew Mar 23 '23 at 14:04

1 Answers1

0

DATA column was Deprecated in API level 29 For picking pdf and other files in android 13 and above you can use library : https://github.com/HBiSoft/PickiT

Below are code samples to use it

First, implement

public class EditCardAddItemActivity implements PickiTCallbacks {

Override necessary methods

 @Override
public void PickiTonUriReturned() {

}

@Override
public void PickiTonStartListener() {

}

@Override
public void PickiTonProgressUpdate(int progress) {

}

@Override
public void PickiTonCompleteListener(String path, boolean wasDriveFile, boolean wasUnknownProvider, boolean wasSuccessful, String Reason) {
   Log.d("onactivityresultttt",">pickPath "+path);
 

    
}

@Override
public void PickiTonMultipleCompleteListener(ArrayList<String> paths, boolean wasSuccessful, String Reason) {

}

PickiTonCompleteListener

will give you a path to the selected file.

Declare and initialize

PickiT pickiT;
 pickiT = new PickiT(this,this,this);

Function to open pdf select screen

private void selectPDF() {
    try {
        String[] MIME_TYPE = {"application/pdf"};
        /*String[] MIME_TYPE =
                {"application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", // .doc & .docx
                        "application/vnd.ms-powerpoint", "application/vnd.openxmlformats-officedocument.presentationml.presentation", // .ppt & .pptx
                        "application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // .xls & .xlsx
                        "text/plain",
                        "application/pdf"};*/
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("*/*");
        intent.putExtra(Intent.EXTRA_MIME_TYPES, MIME_TYPE);
        intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
        intent.setFlags(FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION);
        chooseFileResultLauncher.launch(intent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Below is onActivityResult

ActivityResultLauncher<Intent> chooseFileResultLauncher =  registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                Log.d("onactivityresultttt","> "+result.getResultCode());
                if (result.getResultCode() == Activity.RESULT_OK) {
                    Log.d("onactivityresultttt","> "+result.getData());
                    Intent data = result.getData();
                    if (data != null && data.getData() != null) {
                        try {
                            String oldPath = CommonUtils.getFilePath(data.getData(),EditCardAddItemActivity.this);
                            Log.d("oldpathh","> "+oldPath);
                            pickiT.getPath(data.getData(), Build.VERSION.SDK_INT);
                            //now go to PickiTonCompleteListener()
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });

In the above code line pickiT.getPath(data.getData(), Build.VERSION.SDK_INT); will trigger the PickiTonCompleteListener method.

Parsania Hardik
  • 4,593
  • 1
  • 33
  • 33