2016-12-29 44 views
0

我试图通过调用方法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()

回答

3

在Marshmallow OS及以上版本中,您必须使用此代码读取或写入外部存储上的任何内容。

@Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
          if (checkPermission()) { 
             LoadMyData(Environment.getExternalStorageDirectory().getAbsolutePath()); 

          } else { 
           requestPermission(); 
          } 
         } 
       } 


       protected boolean checkPermission() { 
        int result = ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE); 
        if (result == PackageManager.PERMISSION_GRANTED) { 
         return true; 
        } else { 
         return false; 
        } 
       } 

       protected void requestPermission() { 

        if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.READ_EXTERNAL_STORAGE)) { 
         Toast.makeText(this, "Write External Storage permission allows us to do store images. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show(); 
        } else { 
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
          requestPermissions(new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 100); 
         } 
        } 
       } 

      @Override 
      public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { 
       switch (requestCode) { 
        case 100: 
         if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
          LoadMyData(Environment.getExternalStorageDirectory().getAbsolutePath()); 
         } else { 
          Log.e("value", "Permission Denied, You cannot use local drive ."); 
         } 
         break; 
       } 
      } 
+0

感谢您的迅速回复。它的工作原理 –

2

你有后用户的权限授予调用LoadData() - 你的活动必须实现ActivityCompat.OnRequestPermissionsResultCallback和许可请求的结果将交付给它的onRequestPermissionsResult(int, String[], int[])方法。

+0

感谢您的回复。它有帮助。 –