Using Android Storage Access Framework and direct file access together

1.6k views Asked by At

I have an app that extensively uses file system including external SD card.

Currently it uses File APIs to create/delete/list files but I would like to use new Android Storage Access Framework.

Issue is that almost 60% of install base is still on JB and below and I need new Intent.ACTION_OPEN_DOCUMENT_TREE. I am not quite sure how to go about implementing it.

I thought I would write a common interface to use in my app something like MyFile then use appropriate APIs according to platform version but it will be too much unless there is already a library that does it.

What other people are doing?How do you manage this situation?

1

There are 1 answers

1
Pointer Null On

Make some abstraction of your operations.

If you use File in many places, first replace it by some abstract class of interface such as FileSystem, providing same functionality as File, but hiding implementation details. All work with File has to be removed and placed in just one implementation of your TheFile interface, named for example NativeFileSystem. Implement all needed functions, such as open/delete/rename/create.

Use factory function to create FileSystem for example in your Application class. This is place where you later make decision what is actual class created for the work.

When you test that you hid implementation details and your app works, you can create new implementation named for example SAFFileSystem, and implement needed functions, but in a way how Storage Access Framework allows.

Be prepared that some methods that can be run on File on main thread will probably be asynchronous. This is because SAF may possibly show dialog and ask user for confirmation. So there may be some rework in your app related to method calls.

Hope that helps.