6

Since the changes related to the authorizations of access to the shared storage, it does not seem any more possible to search all the documents of the type pdf by this approach (with requestLegacyExternalStorage = "false"):

ContentResolver cr = context.getContentResolver();
Uri uri = MediaStore.Files.getContentUri("external");
String[] projection = null;
String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
                    + MediaStore.Files.FileColumns.MEDIA_TYPE_NONE;
String[] selectionArgs = null;
String sortOrder = null;
Cursor allNonMediaFiles = cr.query(uri, projection, selection, selectionArgs, sortOrder);

Check this link : Media data restrictions

The only solution I see is to scan in a recurcive way all the tree of the shared storage with SAF, which seems to me very expensive in resources and ridiculous.

Does anyone have another idea?

Aristide13
  • 244
  • 2
  • 16
  • Quote: `The MediaStore.Files table is itself filtered, showing only images, videos, and audio files. For example, the table doesn't show PDF files.`. – blackapps Nov 04 '19 at 10:31
  • @blackapps yes it is the link that I mentioned above – Aristide13 Nov 04 '19 at 11:19
  • 1
    I used SAF picked the primary partition and traversed it blindly searching for pdf files. It took 88 seconds. But restricting to Downloads and Android directory less then two seconds. (I think there are no other places where users could put pdf files.) – blackapps Nov 04 '19 at 11:32
  • FYI: Traversing all of a micro SD card took 44 seconds. – blackapps Nov 04 '19 at 11:33
  • with SAF it can be long (it depends only on the number of files). and yes I already manage a priority to scan download directory ... – Aristide13 Nov 04 '19 at 11:42
  • but to maintain a database up to date ... for example in a jobservice, when we copy a file or folders from a PC ... it's once again a regression! – Aristide13 Nov 04 '19 at 11:47

2 Answers2

2

The basic idea of scoped storage is exactly to avoid this kind of behavior, you can't know if there are or not some files somewhere in the user phone. You can just ask for permission to access to the storage tree and scan everything as you said. Even in this case the user could select a folder different from the root so your app will be limited to that folder. The idea could be perform a scan and then update your database keeping in sync using a job (job service) scheduled on the modification of tree URI and descendants.

greywolf82
  • 21,813
  • 18
  • 54
  • 108
  • What you explain is partially wrong. The starting point is that google has removed non media files from the media store table. No doubt they considered that a pdf file is more sensitive than a photo or a video ?? – Aristide13 Nov 05 '19 at 07:31
1

if your project targeted to Sdk level 29 you should add to your AndroidManifest.xml the flag android:requestLegacyExternalStorage="true" under your <application> tag.

 public List<String> queryFilesFromDevice(Uri uri, String[] projection, String selection, final Context context) {
    final List<String> tempList = new ArrayList<>();
    Cursor c = context.getContentResolver().query(uri, projection,
            selection,
            null,
            null);
    if (c != null) {

        while (c.moveToNext()) {
            String path = c.getString(0);
            long size = c.getLong(1);
         // your code logic should be here
        }
        c.close();
    }
    tempList.add(path);
    return tempList;
}

you need to call the function like this:

String pdfExt = "_data LIKE '%.pdf'";
Uri ducumentsUri = MediaStore.Files.getContentUri("external");
String[] docsProjection ={MediaStore.Files.FileColumns.DATA,MediaStore.Images.Media.SIZE,MediaStore.Files.FileColumns.MIME_TYPE,};
queryFilesFromDevice(ducumentsUri, docsProjection, pdfExt, this);
  • 1
    The question is specifically about doing it android 10. Adding `android:requestLegacyExternalStorage="true"` is not safe as it's a temporary flag and would be removed soon. Also targeting SDK 29 for new and old app is going to be mandatory on play store by end of the year. So please refrain from suggesting this as a solution. – shaktiman_droid Apr 30 '20 at 19:52