Coder Social home page Coder Social logo

ohmysaf's Introduction

Oh My SAF

Android SAF helper.

Implement

  1. Add maven.yuuta.dev repo to your module

  2. Add dependency:

implementation 'moe.yuuta.ohmysaf:ohmysaf:<the latest version>'

Main Features

  • Faster migrate - If you have an old project which uses java.io.File a lot and you would like to migrate to SAF, OMG. By using OhMySAF, you can just continue using File with a little hack.

Faster Migrate

Imagine that you have an old project and its file operation codes look like:

File theFxxkFile = new File("xxx/xxx");
if (theFxxkFile.isFile()) {
    theFxxkFile.delete();
    theFxxkFile.mkdir(); // In fact, SAF does not support that.
}

If you want to switch to SAF, the normal code will look like:

// Assume that you've got the URI
DocumentFile theFxxkFile = DocumentFile.fromTreeUri(context, uri);
if (theFxxkFile.isFile()) {
    theFxxkFile.delete();
    // .....
}

One day you discovered OhMySAF, your problem is solved:

// Assume that you've got the URI

// Magic
File theFxxkFile = OhMySAF.ohMyTree(context, uri);

// Just KEEP these old codes!
if (theFxxkFile.isFile()) {
    theFxxkFile.delete();
    theFxxkFile.mkdir(); // In fact, SAF does not support that.
}

Extend usages

Some APIs don't work with SafFile. In these cases, you should cast the returned File to SafFile and use extended APIs.

SafFile theMagic = OhMySAF.ohMyUri(context, uri);

Get Android Uri

It returns the Android Uri and you can do ANYTHING you want with it.

final Uri uri = theMagic.getAndroidUri();

Open input / output stream

The Java FileInputStream and FileOutputStream does not work with the file directly. Talk is cheap, let's show you the code:

final OutputStream stream = theMagic.openOutputStream(this, "w");
stream.close();

Licenses

Apache 2.0

ohmysaf's People

Contributors

trumeet avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.