简体   繁体   中英

How to add files access permission on device android

My problem is when I am not getting files permission in the device. Below image is showing: photos and media access are showing but files access not showing.

在此处输入图片说明

In the above permission access files permission not showing like below: I need below permissions including files access.

在此处输入图片说明

I use permision in the manifest:

<uses-permission
    android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
    android:name="android.permission.CAMERA" />
    

From the MainActivity:

private static final int CAMERA_PERMISSION_CODE = 100;
private static final int STORAGE_PERMISSION_CODE = 101;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    storage = findViewById(R.id.storage);
    camera = findViewById(R.id.camera);

    // Set Buttons on Click Listeners
    storage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            checkPermission(
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    STORAGE_PERMISSION_CODE);
        }
    });

    camera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            checkPermission(Manifest.permission.CAMERA,
                    CAMERA_PERMISSION_CODE);
        }
    });
}

// Function to check and request permission.
public void checkPermission(String permission, int requestCode)
{
    if (ContextCompat.checkSelfPermission(MainActivity.this, permission)
            == PackageManager.PERMISSION_DENIED) {

        // Requesting the permission
        ActivityCompat.requestPermissions(MainActivity.this,
                new String[] { permission },
                requestCode);
    }
    else {
        Toast.makeText(MainActivity.this,
                "Permission already granted",
                Toast.LENGTH_SHORT)
                .show();
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       @NonNull String[] permissions,
                                       @NonNull int[] grantResults)
{
    super
            .onRequestPermissionsResult(requestCode,
                    permissions,
                    grantResults);

    if (requestCode == CAMERA_PERMISSION_CODE) {
        if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(MainActivity.this,
                    "Camera Permission Granted",
                    Toast.LENGTH_SHORT)
                    .show();
        }
        else {
            Toast.makeText(MainActivity.this,
                    "Camera Permission Denied",
                    Toast.LENGTH_SHORT)
                    .show();
        }
    }
    else if (requestCode == STORAGE_PERMISSION_CODE) {
        if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(MainActivity.this,
                    "Storage Permission Granted",
                    Toast.LENGTH_SHORT)
                    .show();
        }
        else {
            Toast.makeText(MainActivity.this,
                    "Storage Permission Denied",
                    Toast.LENGTH_SHORT)
                    .show();
        }
    }
}
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       @NonNull String[] permissions,
                                       @NonNull int[] grantResults)
{
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == STORAGE_PERMISSION_CODE) {
        if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(MainActivity.this,
                    "Storage Permission Granted",
                    Toast.LENGTH_SHORT)
                    .show();
        }
        else {
            Toast.makeText(MainActivity.this,
                    "Storage Permission Denied",
                    Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

Adding the below line in the Application portion of Manifest

android:requestLegacyExternalStorage="true"

solves the problem.

Changing the targetSdkVersion from 30 to 29 in my build.gradle file changed the dialog for me from photos/media to photos/media/files. I believe it has something to do with how API 30 is more strict with scoped storage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM