aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: e27a01e9696ddadda60e2be5a49f86c61841a147 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Oh My SAF

Android [SAF](https://developer.android.com/guide/topics/providers/document-provider) helper.

# Implement

1. Add [maven.yuuta.dev](https://maven.yuuta.dev) repo to your module

2. Add dependency:

```grooxy
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:

```java
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:

```java
// 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:

```java
// 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.

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

### Get Android Uri

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

```java
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:

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

# Licenses

Apache 2.0