Read and Write external storage permission isn't working

后端 未结 5 1629
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-08 00:58

In my app i want to get user permission to read and write data on external storage, i have put permission tag in my manifest as below. But when installing app or running and whe

5条回答
  •  滥情空心
    2021-02-08 01:28

    Recommended solution: User a permission library like Dexter or KotlinPermissions or RxPermissions. I have used all of them and they are quite reliable.

    Else:

    Write these lines in your activity, just bellow super.onCreate(...) and before setContentView(...):

    super.onCreate(savedInstanceState);
    
        // Here, thisActivity is the current activity
    
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
        }
    
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }
    
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
        }
        setContentView(R.layout.activity_main);
    

    Now it should work, it fixed the problem for me!

提交回复
热议问题