Unzip a file using Scoped Storage in Android

169 views Asked by At

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);
                }
            }
        }
    });
0

There are 0 answers