Search in sources :

Example 1 with MediaStream

use of org.nuclearfog.twidda.backend.api.holder.MediaStream in project Shitter by nuclearfog.

the class ImageLoader method doInBackground.

@Override
protected Boolean doInBackground(Uri[] links) {
    try {
        // download imaged to a local cache folder
        for (Uri link : links) {
            // get input stream
            MediaStream mediaStream = twitter.downloadImage(link.toString());
            InputStream input = mediaStream.getStream();
            String mimeType = mediaStream.getMimeType();
            // create file
            String ext = '.' + mimeType.substring(mimeType.indexOf('/') + 1);
            File imageFile = new File(cacheFolder, StringTools.getRandomString() + ext);
            imageFile.createNewFile();
            // copy image to cache folder
            FileOutputStream output = new FileOutputStream(imageFile);
            int length;
            byte[] buffer = new byte[4096];
            while ((length = input.read(buffer)) > 0) output.write(buffer, 0, length);
            input.close();
            output.close();
            // create a new uri
            publishProgress(Uri.fromFile(imageFile));
        }
        return true;
    } catch (TwitterException err) {
        this.err = err;
    } catch (IOException exception) {
        exception.printStackTrace();
    }
    return false;
}
Also used : MediaStream(org.nuclearfog.twidda.backend.api.holder.MediaStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Uri(android.net.Uri) File(java.io.File) TwitterException(org.nuclearfog.twidda.backend.api.TwitterException)

Example 2 with MediaStream

use of org.nuclearfog.twidda.backend.api.holder.MediaStream in project Shitter by nuclearfog.

the class Twitter method downloadImage.

/**
 * download image from twitter
 *
 * @param link link to the image
 * @return image bitmap
 */
public MediaStream downloadImage(String link) throws TwitterException {
    try {
        // this type of link requires authentication
        if (link.startsWith(DOWNLOAD)) {
            Response response = get(link, new ArrayList<>(0));
            if (response.code() == 200 && response.body() != null) {
                MediaType type = response.body().contentType();
                if (type != null) {
                    String mime = type.toString();
                    InputStream stream = response.body().byteStream();
                    return new MediaStream(stream, mime);
                }
            }
            throw new TwitterException(response);
        } else // public link, no authentication required
        {
            Request request = new Request.Builder().url(link).get().build();
            Response response = client.newCall(request).execute();
            if (response.code() == 200 && response.body() != null) {
                MediaType type = response.body().contentType();
                if (type != null) {
                    String mime = type.toString();
                    InputStream stream = response.body().byteStream();
                    return new MediaStream(stream, mime);
                }
            }
            throw new TwitterException(response);
        }
    } catch (IOException e) {
        throw new TwitterException(e);
    }
}
Also used : Response(okhttp3.Response) MediaStream(org.nuclearfog.twidda.backend.api.holder.MediaStream) InputStream(java.io.InputStream) Request(okhttp3.Request) MediaType(okhttp3.MediaType) IOException(java.io.IOException)

Example 3 with MediaStream

use of org.nuclearfog.twidda.backend.api.holder.MediaStream in project Shitter by nuclearfog.

the class TweetUpdater method doInBackground.

@Override
protected Void doInBackground(TweetUpdate... tweets) {
    TweetUpdate update = tweets[0];
    try {
        // upload media first
        MediaStream[] mediaStreams = update.getMediaStreams();
        long[] mediaIds = new long[mediaStreams.length];
        for (int pos = 0; pos < mediaStreams.length; pos++) {
            // upload media file and save media ID
            mediaIds[pos] = twitter.uploadMedia(mediaStreams[pos]);
        }
        // upload tweet
        if (!isCancelled()) {
            twitter.uploadTweet(update, mediaIds);
        }
    } catch (TwitterException twException) {
        this.twException = twException;
    } finally {
        // close inputstreams
        update.close();
    }
    return null;
}
Also used : MediaStream(org.nuclearfog.twidda.backend.api.holder.MediaStream) TweetUpdate(org.nuclearfog.twidda.backend.api.holder.TweetUpdate) TwitterException(org.nuclearfog.twidda.backend.api.TwitterException)

Aggregations

MediaStream (org.nuclearfog.twidda.backend.api.holder.MediaStream)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 TwitterException (org.nuclearfog.twidda.backend.api.TwitterException)2 Uri (android.net.Uri)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 MediaType (okhttp3.MediaType)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 TweetUpdate (org.nuclearfog.twidda.backend.api.holder.TweetUpdate)1