我正在嘗試將Android N支持添加到我的應用程序。我使用的是新Scoped Directory Access API讀取外部存儲我的應用程序的存儲目錄,比如:作用域目錄訪問API拋出NullPointerException
public void requestExternalStoragePermissions(int requestCode) {
StorageManager sm = (StorageManager)getContext().getSystemService(Context.STORAGE_SERVICE);
StorageVolume volume = sm.getPrimaryStorageVolume();
Intent intent = volume.createAccessIntent("MyApp");
startActivityForResult(intent, requestCode);
}
但是,我得到一個NullPointerException
上startActivityForResult
:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.content.Intent.migrateExtraStreamToClipData()' on a null object reference
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1635)
at android.app.Activity.startActivityForResult(Activity.java:4858)
at android.app.Activity.startActivityFromFragment(Activity.java:4844)
at android.app.Activity$HostCallbacks.onStartActivityFromFragment(Activity.java:7125)
at android.app.Fragment.startActivityForResult(Fragment.java:1150)
at android.app.Fragment.startActivityForResult(Fragment.java:1139)
....
我在做什麼錯?
這些文件必須在文件管理器中方便用戶訪問,所以getExternalFilesDir()
是不是的一個選項。