Skip to content

Latest commit

 

History

History
73 lines (50 loc) · 2.99 KB

JAVA_COMPATIBILITY.md

File metadata and controls

73 lines (50 loc) · 2.99 KB

Java Compatibility

Kotlin is compatible with Java, meaning that Kotlin code is readable in Java.

How to use?

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.

Extension Functions

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() and File.getStorageId() → Get storage ID. Returns primary for external storage and something like AAAA-BBBB for SD card.
  • DocumentFile.getAbsolutePath() → Get file's absolute path. Returns something like /storage/AAAA-BBBB/Music/My Love.mp3.
  • DocumentFile.copyFileTo() and File.copyFileTo()
  • DocumentFile.search() and File.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:

In Kotlin

val file = ...
val storageId = file.getStorageId(context)

In Java

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.

Utility Functions

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.

In Kotlin

val file = DocumentFileCompat.fromSimplePath(context, "AAAA-BBBB", "Music/My Love.mp3")

In Java

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.

Sample Code

  • More sample code in Java can be found in JavaActivity
  • Learn Kotlin on Udacity. It's easy and free!