Kotlin is compatible with Java, meaning that Kotlin code is readable in Java.
Simple Storage contains utility functions stored in object
class, e.g. DocumentFileCompat
and MediaStoreCompat
.
These classes contain only static functions.
Additionally, this library also has extension functions, e.g. DocumentFileExtKt
and FileExtKt
.
You can learn it here.
Common extension functions are stored in package com.anggrayudi.storage.extension
. The others are in com.anggrayudi.storage.file
.
You'll find that the most useful extension functions come from DocumentFileExtKt
and FileExtKt
. They are:
DocumentFile.getStorageId()
andFile.getStorageId()
→ Get storage ID. Returnsprimary
for external storage and something likeAAAA-BBBB
for SD card.DocumentFile.getAbsolutePath()
→ Get file's absolute path. Returns something like/storage/AAAA-BBBB/Music/My Love.mp3
.DocumentFile.copyFileTo()
andFile.copyFileTo()
DocumentFile.search()
andFile.search()
, etc.
Note that some long-running functions like copy, move, search, compress, and unzip are now only available in Kotlin. You can still use these Java features in your project, but you will need v1.5.6 which is the latest version that supports Java.
Suppose that you want to get storage ID of the file:
val file = ...
val storageId = file.getStorageId(context)
DocumentFile file = ...
String storageId = DocumentFileUtils.getStorageId(file, context);
All extension functions work like static methods in Java. Note that since 0.4.2
,
their class names are renamed from using suffix ExtKt
to Utils
.
I will refer to utility functions stored in Kotlin object
class so you can understand it easily.
You can find the most useful utility functions in DocumentFileCompat
and MediaStoreCompat
.
Suppose that I want to get file from SD card with the following simple path: AAAA-BBBB:Music/My Love.mp3
.
BTW, AAAA-BBBB
is the SD card's storage ID for this example.
val file = DocumentFileCompat.fromSimplePath(context, "AAAA-BBBB", "Music/My Love.mp3")
DocumentFile file = DocumentFileCompat.INSTANCE.fromSimplePath(context, "AAAA-BBBB", "Music/My Love.mp3");
In Java, you need to append INSTANCE
after the utility class name.
Anyway, if the function is annotated with @JvmStatic
, you don't need to append INSTANCE
.
Just go to the source code to check whether it has the annotation.
- More sample code in Java can be found in
JavaActivity
- Learn Kotlin on Udacity. It's easy and free!