1
我开发了一个应用程序,安装在Nexus 7 android 5上,它显示SD卡上的目录中的文件列表。升级到Android 6后,它不再可以访问SD卡了!将Nexus 7升级到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
!
EDIT 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());
}
}
这个错误造成的NullPointerException 检查此Ref:http://stackoverflow.com/questions/33495022/app-crashes-on-android-6-0 -launch-logcat-included-new-developer?rq = 1 –
正确,在android 6中,'listFiles()'cusses nullpointerexception错误,但是为什么在android 6中。而且通过应用链接中提出的修改,问题没有解决 – Behy