Android library for abstract file-system access required by Android Q
Flipper is an android library project to help developers to use file-like interface using the storage access framework. Flipper is designed to handle the new behavior required by Android Q.
Set up the project dependencies. To use this library in your project:
(1) Use the GitHub source and include that as a module dependency by following these steps:
git clone https://github.com/baldapps/Flipper.git
include ':flipper'
project(':flipper').projectDir = new File('../Flipper/flipper')
dependencies {
...
compile project(':flipper')
}
Now your project is ready to use this library
Flipper provides an abstaction layer to manage files using Uri access on Android platform.
Example and how to use the library:
public class MainActivity extends Activity {
private StorageManagerCompat manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = new StorageManagerCompat(this);
Root root = manager.getRoot(StorageManagerCompat.DEF_MAIN_ROOT);
if (root == null || !root.isAccessGranted(this)) {
Intent i = manager.requireExternalAccess(this);
startActivityForResult(i, 100);
}
}
private void myMethod() {
Root root = manager.getRoot(StorageManagerCompat.DEF_MAIN_ROOT);
if (root == null)
return;
DocumentFile f = root.toRootDirectory(this);
if (f == null)
return;
DocumentFile subFolder = DocumentFileCompat.peekSubFolder(f, "mysub");
if (subFolder == null)
return; //directory doesn't exist yet
DocumentFile myFile = DocumentFileCompat.peekFile(subFolder, "myfile", "image/png");
if (myFile == null)
return; //file doesn't exist yet
try {
InputStream is = getContentResolver().openInputStream(myFile.getUri());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100 && resultCode == RESULT_OK) {
Root root = manager.addRoot(this, StorageManagerCompat.DEF_MAIN_ROOT, data);
if (root == null)
return;
DocumentFile f = root.toRootDirectory(this);
if (f == null)
return;
try {
DocumentFile subFolder = DocumentFileCompat.getSubFolder(f, "mysub");
DocumentFile myFile = DocumentFileCompat.getFile(subFolder, "myfile", "image/png");
try {
OutputStream os = getContentResolver().openOutputStream(myFile.getUri());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} catch (OperationFailedException e) {
e.printStackTrace();
}
}
}
See LICENSE
1.0.0