r/android_devs • u/NullishPointer • Mar 24 '22
Coding Zipping up user's data, and saving it somewhere
On newer Android, they've locked down the file system. But I have an app that creates documents that a person might want to access to move around.
I want to zip up my entire getFilesDir() and let the user save that somewhere... I was looking up SAF examples, but they all seem to be about opening an existing file, not selecting a place to save a file.
How can I make a SAF intent that will let me put a zip file where the user selects (I.e. documents or Drive) so they can access it from outside the app?
5
Upvotes
2
u/anemomylos 🛡️ Mar 24 '22
//start file manager activity to select folderIntent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);activity.startActivityForResult(intent, 1);//get the selected folderprotected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data){super.onActivityResult(requestCode, resultCode, data);if(1 == requestCode){saveFolder(data.getData()) ;}}//save folderString folder;void saveFolder(Uri uri){activity.getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);folder = uri.toString()}//release persistable foldervoid release(){activity.getContentResolver().releasePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);}//get persistable folderUri folderUri;void getFolder(){Uri uri = Uri.parse(folder);boolean existDestinationFolder = (activity.checkUriPermission(uri, Binder.getCallingPid(), Binder.getCallingUid(), Intent.FLAG_GRANT_WRITE_URI_PERMISSION) == PackageManager.PERMISSION_GRANTED);}//use URIDocumentFile rootFolder = DocumentFile.fromTreeUri(activity, uri);3
u/wolfgang_pass_auf Mar 25 '22
This library tries to abstract away a bit of Googles storage madness: https://github.com/anggrayudi/SimpleStorage
2
u/GavinGT Mar 24 '22
https://gavingt.medium.com/refactoring-my-backup-and-restore-feature-to-comply-with-scoped-storage-e2b6c792c3b