Example usage for twitter4j AlternativeHttpClientImpl createInputStreamRequestBody

List of usage examples for twitter4j AlternativeHttpClientImpl createInputStreamRequestBody

Introduction

In this page you can find the example usage for twitter4j AlternativeHttpClientImpl createInputStreamRequestBody.

Prototype

public static RequestBody createInputStreamRequestBody(final MediaType mediaType,
            final InputStream inputStream) 

Source Link

Usage

From source file:com.github.moko256.twitlatte.model.impl.mastodon.PostTweetModelImpl.java

License:Apache License

@Override
public Completable postTweet() {
    return Completable.create(subscriber -> {
        try {/*from   ww  w . j a  v  a 2 s  . c om*/
            ArrayList<Long> ids = null;
            if (uriList.size() > 0) {
                ids = new ArrayList<>(uriList.size());
                for (Uri uri : uriList) {
                    InputStream image = contentResolver.openInputStream(uri);
                    String name = uri.getLastPathSegment();
                    Attachment attachment = new Media(((MastodonTwitterImpl) GlobalApplication.twitter).client)
                            .postMedia(MultipartBody.Part.createFormData("file", name,
                                    AlternativeHttpClientImpl.createInputStreamRequestBody(
                                            MediaType.parse(
                                                    Objects.requireNonNull(contentResolver.getType(uri))),
                                            image)),
                                    null, null)
                            .execute();
                    ids.add(attachment.getId());
                }
            }
            new Statuses(client)
                    .postStatus(tweetText, inReplyToStatusId == -1 ? null : inReplyToStatusId, ids,
                            possiblySensitive, contentWarning.isEmpty() ? null : contentWarning,
                            com.sys1yagi.mastodon4j.api.entity.Status.Visibility.valueOf(visibility), null)
                    .execute();
            subscriber.onComplete();
        } catch (NullPointerException | Mastodon4jRequestException e) {
            subscriber.tryOnError(e);
        }
    });
}