Why File.listFiles() method can only list directories

Viewed 784

I'm stucking with a really wired problem on Android R. Here is what I did.

  1. Claim that I would use WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permissions in Manifest.xml.
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  1. Check and request permissions in my code
   private void checkPermission() {
       int pid = android.os.Process.myPid();
       int read = checkPermission(Manifest.permission.READ_EXTERNAL_STORAGE, pid, 1);
       int write = checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, pid, 1);
       ArrayList<String> permissions = new ArrayList<>();
       if (read != PackageManager.PERMISSION_GRANTED)
           permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
       if (write != PackageManager.PERMISSION_GRANTED)
           permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
       if (permissions.size() > 0) {
           String[] permissionArray = new String[permissions.size()];
           permissions.toArray(permissionArray);
           requestPermissions(permissionArray,1);
       }
   }

   public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                          @NonNull int[] grantResults) {
       //I ignored the result check here since I definitely know that I clicked on the "Allow" button when
       //I was asked to allow the permissions
       File directory = new File("/storage/emulated/0/ebook");
       File[] files = directory.listFiles();
       for(File file : files)
       {
           try {

               Log.i("tagd", "file name: " +file.getCanonicalPath());

           } catch (IOException e) {
               e.printStackTrace();

           }
       }
   }

OK, here comes the wired thing. From the log, I got only one line:

file name: /storage/emulated/0/ebook/temp

But in the folder "ebook" there are actually 40 items. 39 PDF documents and 1 folder "temp". All PDF documents are missing. Same thing happened in other directories with any type of file.

generic_x86_64_arm64:/sdcard/ebook $ ls
1.pdf                                                       dovado\ 4GR\ Reference_manua62X.pdf.pdf
111(1).pdf                                                  fit_diff_pages.pdf
2013\ Mar.pdf                                               help.pdf
2017-08-03_SZ_NK.pdf                                        interactiveform_enabled.pdf
223622-100518.pdf                                           invoice\ 2013-1-28.pdf
59df269384986.pdf                                           pass-111111.pdf
732101500002013-07-21_03-55-53-3352579.pdf                  pdf_commenting_new.pdf
Aeroplane\ -\ bjo.pdf                                       pdfa_pagina_a4_gear_movie.pdf
BoardMeetingMinutes.1024.pdf                                signature.pdf
CreditCardReckoning201211.pdf                               temp
DEMO_SV_lowsize.pdf                                         test.pdf
Geografie_\ Gr\ 10\ Portefeuljeboek\ -\ vnull.pdf           test.pfx
Geografie_\ Gr\ 10\ Portefeuljeboek\ -\ vnull_unlocked.pdf  test1.pdf
Layali_Ashar[1].pdf                                         test28k.pdf
PDF\ markup\ design-Model.pdf                               test_att.pdf
Praise\ And\ Thanksgiving\ (Morning\ Has\ Broken).pdf       visa-china.pdf
Professional_Android_2_Application_Development.pdf          为人父母.pdf
Welcome_to_Radaee_PDF_Master.pdf                            张霁\ 简历\ 3\ -\ 副本.pdf
Xaml_Introduction.pdf                                       比亚迪.pdf
__ViewPoint\ 8000________________\ (2).pdf                  美时每客CakeTime移动平台项目开发分析报告.pdf

The issue can only be reproduced on Android Q. The code is really simple and works fine on other Android releases. Any one got any idea for what happened here? Any help would be appreciated, thank you.

4 Answers

Seems this is related to android 11 storage update. That's a very annoying change. They silently breaks the API contract without much useful error information provided. Either throw exception or add some warning in the log will provide much better clue to save developers times.

https://developer.android.com/about/versions/11/privacy/storage

I had a similar issue. restarting the device solved it.. So weird.. maybe its an android bug

Java File.listFiles() method Returns an array of abstract path names denoting the files in the directory denoted by this abstract path name.

The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

but you can try something like this

for (File file: files) {
        try {

            Log.i("file name" +file.getCanonicalPath()+"/n");

        } catch (IOException e) {
            e.printStackTrace();

        }
Related