Skip to content

Android

The Android SDK is designed to work in Android mobile apps written in Java or Kotlin.

Apps running the Android SDK run an on device Textile peer (including a managed IPFS peer). The SDK provides you with a number of helpful APIs to manage sharing, contacts, file encryption & storage, and much more.

Below are some basic examples to get you started. If you are interested in a more thorough walk-through, check out the Tour of Textile's examples using Java or jump to the full API documentation if you are ready.

Installation

The Textile Android library is published in Textile's Bintray Maven repository. The library source is published on GitHub.

Gradle

First, you'll need to add Textile's Bintray Maven repository to you project's top level build.gradle in the allProjects.repositories section:

allprojects {
    repositories {
        ...
        maven { url "https://dl.bintray.com/textile/maven" }
        ...
    }
}

Next, add the Textile dependency to your app module's build.gradle dependencies section, specifying the latest version available:

dependencies {
    ...
    implementation 'io.textile:textile:0.1.20'
    ...
}

Getting Started

Initialize Textile

Then, in your application’s MainActivity.java, setup Textile as follows,

String recoveryPhrase = Textile.initialize(this.getApplicationContext(), false, false);
if (recoveryPhrase != nil) {
    // Return phrase to the user for secure, out of app, storage
}

class MyEventListener extends BaseTextileEventListener {
    // ...
}

Textile.instance().addEventListener(new MyEventListener());

By default, the Textile instance will manage itself during app life-cycle events as long as you initialize it correctly in your app, meaning your app will be connected to the IPFS network whenever your app requires it.

Get the account display name

String name = Textile.instance().profile.name();

Subscribe to file updates

// Add an event listener that conforms to TextileEventListener
class MyEventListener extends BaseTextileEventListener {
    @Override
    public void threadUpdateReceived(io.textile.pb.View.FeedItem feedItem) {
        if (feedItem.getPayload().getTypeUrl() == "/Files") {
            // new file!
        }
    }
}
Textile.instance().addEventListener(new MyEventListener());

Fetch chronological thread updates

FeedRequest request = FeedRequest.newBuilder()
      .setThread("<thread-id>")
      .build();
FeedItemList feedItemList = Textile.instance().feed.list(request);

Create a thread

AddThreadConfig.Schema schema = AddThreadConfig.Schema.newBuilder()
        .setPreset(AddThreadConfig.Schema.Preset.BLOB)
        .build();
AddThreadConfig config = AddThreadConfig.newBuilder()
        .setKey("your.bundle.id.version.Basic")
        .setName("Basic")
        .setType(Thread.Type.PRIVATE)
        .setSharing(Thread.Sharing.NOT_SHARED)
        .setSchema(schema)
        .build();
Textile.instance().threads.add(config);

Add a file

final String threadId = "<thread-id>";
byte[] jsonData = "{ latitude: 48.858093, longitude: 2.294694 }".getBytes();
String dataString = Base64.encodeToString(jsonData, Base64.DEFAULT);
Textile.instance().files.prepare(dataString, threadId, new PreparedFilesHandler() {
    @Override
    public void onFilesPrepared(MobilePreparedFiles preparedFiles) {
        try {
            Textile.instance().files.add(preparedFiles.getDir(), threadId, null);
        } catch (Exception e) {
            // Do something with this error
        }
    }

    @Override
    public void onError(Exception e) {
        // Do something with this error
    }
});

Files stored on disk

You can add files stored on disk by passing the path information.

final String threadId = "<thread-id>";
String filePath = "/path/to/mmm-bytes....";
Textile.instance().files.prepareByPath(filePath, threadId, new PreparedFilesHandler() {
    @Override
    public void onFilesPrepared(MobilePreparedFiles preparedFiles) {
        try {
            Textile.instance().files.add(preparedFiles.getDir(), threadId, null);
        } catch (Exception e) {
            // Do something with this error
        }
    }

    @Override
    public void onError(Exception e) {
        // Do something with this error
    }
});

Get a file

String hash = "some file hash";
String base64 = Textile.instance().files.data(hash);

Example application

The android-textile repo contains a simple code example you may find useful in your exploration.

API Documentation

The full API documentation for Textile's Android SDK (android-textile) can be found at https://textileio.github.io/android-textile.

Feel free to join the Textile Developer Slack and let us know what you are building. People are always excited to share and learn about new ideas.