我試圖通過調用方法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()
感謝您的迅速回復。它的工作原理 –