我开发了一个安装在Nexus 7 android 5上的应用程序,它显示了SD卡上目录中的文件列表。升级到Android 6后,它再也无法访问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());
}
}
答案 0 :(得分:1)
在API 23中,有一个Runtime权限选项。 您可以查看此代码授予的权限。
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED){
//place your code here
}else{
//request for runtime permission
}
有关运行时权限的更多信息,请阅读this