Search in sources :

Example 1 with Media

use of com.keylesspalace.tusky.entity.Media in project Tusky by Vavassor.

the class ComposeActivity method uploadMedia.

private void uploadMedia(final QueuedMedia item) {
    item.readyStage = QueuedMedia.ReadyStage.UPLOADING;
    final String mimeType = getContentResolver().getType(item.uri);
    MimeTypeMap map = MimeTypeMap.getSingleton();
    String fileExtension = map.getExtensionFromMimeType(mimeType);
    final String filename = String.format("%s_%s_%s.%s", getString(R.string.app_name), String.valueOf(new Date().getTime()), randomAlphanumericString(10), fileExtension);
    byte[] content = item.content;
    if (content == null) {
        InputStream stream;
        try {
            stream = getContentResolver().openInputStream(item.uri);
        } catch (FileNotFoundException e) {
            Log.d(TAG, Log.getStackTraceString(e));
            return;
        }
        content = inputStreamGetBytes(stream);
        IOUtils.closeQuietly(stream);
        if (content == null) {
            return;
        }
    }
    RequestBody requestFile = RequestBody.create(MediaType.parse(mimeType), content);
    MultipartBody.Part body = MultipartBody.Part.createFormData("file", filename, requestFile);
    item.uploadRequest = mastodonAPI.uploadMedia(body);
    item.uploadRequest.enqueue(new Callback<Media>() {

        @Override
        public void onResponse(Call<Media> call, retrofit2.Response<Media> response) {
            if (response.isSuccessful()) {
                onUploadSuccess(item, response.body());
            } else {
                Log.d(TAG, "Upload request failed. " + response.message());
                onUploadFailure(item, call.isCanceled());
            }
        }

        @Override
        public void onFailure(Call<Media> call, Throwable t) {
            Log.d(TAG, t.getMessage());
            onUploadFailure(item, false);
        }
    });
}
Also used : InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) Media(com.keylesspalace.tusky.entity.Media) StringUtils.randomAlphanumericString(com.keylesspalace.tusky.util.StringUtils.randomAlphanumericString) MimeTypeMap(android.webkit.MimeTypeMap) Date(java.util.Date) MultipartBody(okhttp3.MultipartBody) RequestBody(okhttp3.RequestBody)

Aggregations

MimeTypeMap (android.webkit.MimeTypeMap)1 Media (com.keylesspalace.tusky.entity.Media)1 StringUtils.randomAlphanumericString (com.keylesspalace.tusky.util.StringUtils.randomAlphanumericString)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 Date (java.util.Date)1 MultipartBody (okhttp3.MultipartBody)1 RequestBody (okhttp3.RequestBody)1