将 Nexus 7 升级到 android 6,我的应用无法再访问 SD 卡

upgrade Nexus 7 to android 6, my app cannot access to sdcard anymore

我开发了一个应用程序,安装在 Nexus 7 android 5 上,它显示了 sd 卡上目录中的文件列表。升级到Android6后,无法访问sd卡了!

这是我的代码:

    File f = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    String ProjectPath = f.getPath();
    Log.w("path",ProjectPath);
    File list[]=f.listFiles();
    if(list.length>0) {
        for (int i = 0; i < list.length; i++)
            Log.w("path", list[i].getName());
    }

还添加了以下权限:

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

Logcat

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) 

编辑 1
File list[]=f.listFiles();之后,list就是null!

编辑 2
我已按如下方式更正我的代码,它适用于 Android.M 以及以前的版本:

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>=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>0 && 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());
    }
}

在API23中有一个运行时权限的选项。 您可以检查是否使用此代码授予了权限。

     if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED){
    //place your code here
        }else{
//request for runtime permission
              }

有关运行时权限的更多信息,请阅读 this