我试图通过调用方法LoadData()
来从onCreate
方法的外部存储中读取文件。这是我的代码。从onCreate上的棉花糖和以上棉花糖设备上读取外部存储中的文件
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//I tried declaring permissions before calling the file
// ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
// LoadData(Environment.getExternalStorageDirectory().getAbsolutePath());
LoadMyData(Environment.getExternalStorageDirectory().getAbsolutePath());
ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
这是应该如何去: 1.Open the app for the first time --> Ask for permissions --> LoadData(); 2.Open the app for the secondtime --> LoadData();
这是怎么回事: 1.Open the app for the first time --> Ask for permissions ;(It is not calling LoadData()) 2.Open the app for the second time --> LoadData();(For the second time it is calling LoadData())
所有运行时的权限是正确声明。我知道LoadData()
不是第一次调用,因为权限尚未设置。有没有一种方法,因为我想要调用LoadData()onCreate()
感谢您的迅速回复。它的工作原理 –