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;
}
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);
}
}
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;
}
Aggregations