2

I'm creating an Android app which I need to unzip a file in Android/data folder, I'm already done with asking permissions to write at this folder using Scoped Storage with getContentResolver().takePersistableUriPermission() method. My question is given an Uri object representing a subfolder located in Android/data, how can I extract the zip contents in it?

I tried using default android Zip lib and Zip4j but I'm getting permission errors (I think that is related to the path).

EDIT: I don't want to unzip on the app specific data folder but in another app folder in Android/data. This is why I used Scoped Storage.

EDIT 2: I used used this code to get access to write at Android/data:

StorageManager sm = (StorageManager) getApplicationContext().getSystemService(Context.STORAGE_SERVICE);
Intent intent = sm.getPrimaryStorageVolume().createOpenDocumentTreeIntent();

String startSubDir = "Android%2Fdata";
Uri uri = intent.getParcelableExtra("android.provider.extra.INITIAL_URI");
String scheme = uri.toString();

scheme = scheme.replace("/root/", "/document/");
scheme += "%3A" + startSubDir;

uri = Uri.parse(scheme);
intent.putExtra("android.provider.extra.INITIAL_URI", uri);

someActivityResultLauncher.launch(intent);

And onActivityResult() I requested for persistent permission:

ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
    new ActivityResultContracts.StartActivityForResult(),
    new ActivityResultCallback<ActivityResult>() {
        @Override
        public void onActivityResult(ActivityResult result) {
            if (result.getResultCode() == Activity.RESULT_OK) {
                // There are no request codes
                Intent data = result.getData();
                if (data != null) {
                    getContentResolver().takePersistableUriPermission(data.getData(), Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                    
                } else {
                    System.exit(0);
                }
            }
        }
    });
  • You do not need any permission to create files in that app specific directory. – blackapps Dec 24 '22 at 15:37
  • And it is unclear why you would have an uri for a directory there as there are no ways to obtain one. – blackapps Dec 24 '22 at 15:38
  • @blackapps In android 11 or superior we can't access Android/data directly, this is why I used Scoped Storage to get access to this folder. I mentioned the Uri because I got an object of this type when acquiring access, using absolute path like "/storage/emulated/0/Android/data" gives me EACESS error, even with the permission granted. – Warley Soares Dec 24 '22 at 16:16
  • You are talking nonsense. You do not need any permission to go to getExternalFilesDir(). Use that. Do not try to use /storage/emulated/0/Android/data directly but the app specific directory in it. Which you obtain by getExternalFilesDir(null). – blackapps Dec 24 '22 at 17:14
  • @blackapps I think it was not clear in the question that I don't want to unzip in the app specific folder located in Android/data, but in the data folder itself, or saying clearly, on another app data folder, this is why I used Scoped Storage to get access. – Warley Soares Dec 24 '22 at 18:05
  • You have no access to data folder directly and not to another apps folder there. And you did not use scoped storage at all! And if you think you did then show the used code. And remember: you were already three timemes told you cannot get access – blackapps Dec 24 '22 at 19:34
  • @blackapps I added the code. Asking for permission I can write at another app data folder. This is the only way I know for doing that on Android 11 +. – Warley Soares Dec 25 '22 at 20:24
  • That is my code. And it does not work for Android 13 anymore and it has nothing to do with scoped storage. As it does not work for Android 13 anymore better not rely on it. Dont use it. – blackapps Dec 25 '22 at 21:42

0 Answers0