0

I got an app to read pdf files it works fine until i installed android 12 on my device.

Manifest:

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:requestLegacyExternalStorage="true">
...
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" tools:remove="android:maxSdkVersion" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:remove="android:maxSdkVersion"
            tools:ignore="ScopedStorage" />

Permission:

private static final int REQUEST_PERMISSIONS = 20;

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


            // Permission einholen
            PermissionActivity.super.requestAppPermissions(new
                            String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE}, R.string
                            .runtime_permissions_txt
                    , REQUEST_PERMISSIONS);

        }

        @Override
        public void onPermissionsGranted(int requestCode) {
            startActivity(new Intent(PermissionActivity.this, MainActivity.class));
            finish();

        }

And the Code :

File myFile2 = new File(Environment.getExternalStorageDirectory().getPath()+"/FploData/");
       
        if (!myFile2.exists()) {myFile2.mkdir();} // works with Android 12
       
        File lister = myFile2.getAbsoluteFile();
        Log.e("file2: ", String.valueOf(lister));

        File[] files = myFile2.listFiles();
        Log.e("file length: ", String.valueOf(files.length));

files.length returns 0 but the directory isn“t empty. The Code worked fine until i updated my Device from Android 11 to 12. I assume there is something wrong with the permission. Can someone help?

Ruli
  • 2,592
  • 12
  • 30
  • 40

1 Answers1

1

requestLegacyExternalStorage won't work on new installation targetting sdk 30.

Android 10 devices requestLegacyExternalStorage will continue to work regardless of target sdk.

Android 11 devices new installation targetting sdk 29: requestLegacyExternalStorage value is respected.

Android 12 devices new installation targetting sdk 30: requestLegacyExternalStorage is always false.

After august 2021 play store requires target sdk 30 so you're pretty much forced to implement scoped storage at this point.

Himanshi Thakur
  • 2,077
  • 14
  • 32