Android 11 and higher: Create a zip file via Scoped Storage

265 views Asked by At

From Android 11, scoped storage must be used.

How do I create a zip file with my app, which is then saved in e.g. Documents?

The .txt. Files to be zipped are in the internal app storage

I can create zip files, but I can't manage the zip file via "Scoped Storage" in the

save to the public "Downloads" folder.

Would be grateful for any help...

Regards

Herman

@Override

public void onClick(View v) {

String dateiname="Backup.zip";

Intent intentm = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intentm.addCategory(Intent.CATEGORY_OPENABLE);
intentm.setType("application/zip");
intentm.putExtra(Intent.EXTRA_TITLE, dateiname);
someActivityResultLauncher.launch(intentm);

}

 private void createDocument(Uri uri) throws FileNotFoundException {

 


   getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
    String inputPath1 = getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath();
    String inputFile = "Backup_Stromverbrauch.zip";


    String pfad1 = getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS).toString();





    String []s=new String[1];//String Array, Anzahl Dateien z

        s[0] = inputPath1 +  "/Test.txt";

    String xxx= String.valueOf(Uri.parse(uri.toString()));
    Toast.makeText(MainActivity.this, xxx, Toast.LENGTH_LONG).show();




    try {

        ParcelFileDescriptor fileDescriptor = this.getContentResolver().openFileDescriptor(uri, "w");
        FileOutputStream fileOutputStream =
                new FileOutputStream(fileDescriptor.getFileDescriptor());

        ZipManager zipManager = new ZipManager();
        zipManager.zip(s, String.valueOf(fileOutputStream));
        

    } catch (Exception e) {

    }
0

There are 0 answers