title: upgrade Nexus 7 to android 6, my app cannot access to sdcard anymore
body: <p>I have developed an app, installed on Nexus 7 android 5, which shows list of files in a directory on sd card. After upgrade to Android 6, it canot access to sd card anymore!</p>
  
  <p>Here my code:</p>
  
  <pre><code>    File f = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
      String ProjectPath = f.getPath();
      Log.w("path",ProjectPath);
      File list[]=f.listFiles();
      if(list.length&gt;0) {
          for (int i = 0; i &lt; list.length; i++)
              Log.w("path", list[i].getName());
      }
  </code></pre>
  
  <p>following permissions also added: </p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" /&gt;
  </code></pre>
  
  <p><b> Logcat</b></p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
          Process: com.behy.myapplication, PID: 13472
          java.lang.RuntimeException: Unable to start activity ComponentInfo{com.behy.myapplication/com.behy.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to get length of null array
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
          at android.app.ActivityThread.-wrap11(ActivityThread.java)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:148)
          at android.app.ActivityThread.main(ActivityThread.java:5417)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
          Caused by: java.lang.NullPointerException: Attempt to get length of null array
          at com.behy.myapplication.MainActivity.onCreate(MainActivity.java:21)
          at android.app.Activity.performCreate(Activity.java:6251)
          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
          at android.app.ActivityThread.-wrap11(ActivityThread.java) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
          at android.os.Handler.dispatchMessage(Handler.java:102) 
          at android.os.Looper.loop(Looper.java:148) 
          at android.app.ActivityThread.main(ActivityThread.java:5417) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
  </code></pre>
  
  <p><b> EDIT 1</b><br>
  After <code>File list[]=f.listFiles();</code>, <code>list</code> is <code>null</code>!</p>
  
  <p><b> EDIT 2</b><br>
  I've corrected my code as follows and it works for Android.M as well as previous versions: </p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Log.w("path","ver: " + android.os.Build.VERSION.SDK_INT);
          if (android.os.Build.VERSION.SDK_INT&gt;=Build.VERSION_CODES.M) {
              if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
                  readFile();
              else
                  requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 2909);
          }
          else
              readFile();
      }
      @Override
      public  void onRequestPermissionsResult(int requestCode, String permission[], int[] grantesult){
          if(requestCode == 2909){
              if(grantesult.length&gt;0 &amp;&amp; grantesult[0]==PackageManager.PERMISSION_GRANTED)
                  readFile();
          }
      }
      private void readFile(){
              File f = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
              String ProjectPath = f.getPath();
              Log.w("path",ProjectPath);
              File list[]=f.listFiles();
              if(!f.exists() )return;
              for(File file:f.listFiles())
                  Log.w("path", file.getName());
      }
  }
  </code></pre>