上面的Android版本4.3 listFiles()总是返回Null为什么?

时间:2017-03-12 18:49:04

标签: java android

我创建了一个文件管理器应用程序,它将SDCard中的异步文件列入循环器视图。

      private class AsyncFileListLoader extends AsyncTask<String, Void, Boolean> {
        protected void onPreExecute() {
        //Shows the Custom Progress Dialog
        }
         @Override
                protected Boolean doInBackground(String... param) {

         File oneFile=new File(mother_folder);

        /*This code causes the App to Crash by Null pointer exception. 
        Happens only in Android ver 4.3 and above.WHY??*/
        File[] files = oneFile.listFiles();

         if(files.length ==0) return false;
    /*  THIS IS ALWAYS FALSE ONLY IN VERSION 4.3 and above WHY??*/

        }
        ///Rest of the code///

        }
        }
Executed thus
String DirPath=Environment.getExternalStorageDirectory().getAbsolutePath();

/* Correctly displays the path to External storage==Sdcard or Emulated in ver 4.4 and above*/

Toast.makeText(this,DirPath, Toast.LengthShort).show();

/* causes app to crash
 new AsyncFileListLoader().execute(DirPath);

我已在Android清单中设置了正确的权限,并填充了列表 Android版4.2电话。我检查了SD卡的运行时间权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

根据此主题使用RUN时间权限 listFiles() returns null on Android 6.0 emulator

int permissionCheck1 = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
    int permissionCheck2 = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (permissionCheck1 != PackageManager.PERMISSION_GRANTED || permissionCheck2 != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                             Manifest.permission.READ_EXTERNAL_STORAGE},
                REQUEST_READWRITE_STORAGE);
    }

检查自我权限返回true。我有权读取和写入外部存储空间。经过1周的搜索,我没有解决方案。这个

File[] files = oneFile.listFiles();

总是返回null,加上使用FileUtils.listFiles DOES NOT RETURN null为什么?在内部,我在这里检查了源代码,FileUtils使用相同的ListFile ...

https://commons.apache.org/proper/commons-io/javadocs/api-2.5/src-html/org/apache/commons/io/FileUtils.html

似乎没有其他功能可以替换listfiles()..

提前感谢一堆。

0 个答案:

没有答案
相关问题