mirror of
https://github.com/TwidereProject/Twidere-Android
synced 2025-02-01 17:26:46 +01:00
added scale image
This commit is contained in:
parent
830ffb7c7a
commit
738bdb1acc
@ -231,7 +231,7 @@ class UpdateStatusTask(internal val context: Context, internal val stateCallback
|
|||||||
val sizeLimit = Point(2048, 1536)
|
val sizeLimit = Point(2048, 1536)
|
||||||
body = getBodyFromMedia(context.contentResolver,
|
body = getBodyFromMedia(context.contentResolver,
|
||||||
Uri.parse(statusUpdate.media[0].uri),
|
Uri.parse(statusUpdate.media[0].uri),
|
||||||
sizeLimit,
|
sizeLimit, statusUpdate.media[0].type,
|
||||||
ContentLengthInputStream.ReadListener { length, position ->
|
ContentLengthInputStream.ReadListener { length, position ->
|
||||||
stateCallback.onUploadingProgressChanged(-1, position, length)
|
stateCallback.onUploadingProgressChanged(-1, position, length)
|
||||||
})
|
})
|
||||||
@ -274,16 +274,14 @@ class UpdateStatusTask(internal val context: Context, internal val stateCallback
|
|||||||
private fun uploadMediaWithDefaultProvider(update: ParcelableStatusUpdate, pendingUpdate: PendingStatusUpdate) {
|
private fun uploadMediaWithDefaultProvider(update: ParcelableStatusUpdate, pendingUpdate: PendingStatusUpdate) {
|
||||||
// Return empty array if no media attached
|
// Return empty array if no media attached
|
||||||
if (ArrayUtils.isEmpty(update.media)) return
|
if (ArrayUtils.isEmpty(update.media)) return
|
||||||
val ownersList = ArrayList<UserKey>()
|
val ownersList = update.accounts.filter {
|
||||||
val ownerIdsList = ArrayList<String>()
|
ParcelableAccount.Type.TWITTER == ParcelableAccountUtils.getAccountType(it)
|
||||||
for (item in update.accounts) {
|
}.map {
|
||||||
if (ParcelableAccount.Type.TWITTER == ParcelableAccountUtils.getAccountType(item)) {
|
it.account_key
|
||||||
// Add to owners list
|
|
||||||
ownersList.add(item.account_key)
|
|
||||||
ownerIdsList.add(item.account_key.id)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
val ownerIds = ownerIdsList.toTypedArray()
|
val ownerIds = ownersList.map {
|
||||||
|
it.id
|
||||||
|
}.toTypedArray()
|
||||||
for (i in 0..pendingUpdate.length - 1) {
|
for (i in 0..pendingUpdate.length - 1) {
|
||||||
val account = update.accounts[i]
|
val account = update.accounts[i]
|
||||||
val mediaIds: Array<String>?
|
val mediaIds: Array<String>?
|
||||||
@ -420,9 +418,9 @@ class UpdateStatusTask(internal val context: Context, internal val stateCallback
|
|||||||
try {
|
try {
|
||||||
val sizeLimit = Point(2048, 1536)
|
val sizeLimit = Point(2048, 1536)
|
||||||
body = getBodyFromMedia(context.contentResolver, Uri.parse(media.uri), sizeLimit,
|
body = getBodyFromMedia(context.contentResolver, Uri.parse(media.uri), sizeLimit,
|
||||||
ContentLengthInputStream.ReadListener { length, position ->
|
media.type, ContentLengthInputStream.ReadListener { length, position ->
|
||||||
stateCallback.onUploadingProgressChanged(index, position, length)
|
stateCallback.onUploadingProgressChanged(index, position, length)
|
||||||
})
|
})
|
||||||
if (chucked) {
|
if (chucked) {
|
||||||
resp = uploadMediaChucked(upload, body, ownerIds)
|
resp = uploadMediaChucked(upload, body, ownerIds)
|
||||||
} else {
|
} else {
|
||||||
@ -660,24 +658,41 @@ class UpdateStatusTask(internal val context: Context, internal val stateCallback
|
|||||||
fun getBodyFromMedia(resolver: ContentResolver,
|
fun getBodyFromMedia(resolver: ContentResolver,
|
||||||
mediaUri: Uri,
|
mediaUri: Uri,
|
||||||
sizeLimit: Point? = null,
|
sizeLimit: Point? = null,
|
||||||
|
@ParcelableMedia.Type type: Int,
|
||||||
readListener: ContentLengthInputStream.ReadListener): FileBody {
|
readListener: ContentLengthInputStream.ReadListener): FileBody {
|
||||||
val mediaType = resolver.getType(mediaUri)
|
var mediaType = resolver.getType(mediaUri)
|
||||||
val st = resolver.openInputStream(mediaUri) ?: throw FileNotFoundException(mediaUri.toString())
|
|
||||||
val cis: ContentLengthInputStream
|
val cis: ContentLengthInputStream
|
||||||
val length: Long
|
val length: Long
|
||||||
if (sizeLimit != null) {
|
if (type == ParcelableMedia.Type.IMAGE && sizeLimit != null) {
|
||||||
val o = BitmapFactory.Options()
|
val o = BitmapFactory.Options()
|
||||||
o.inJustDecodeBounds = true
|
o.inJustDecodeBounds = true
|
||||||
BitmapFactory.decodeStream(st, null, o)
|
BitmapFactoryUtils.decodeUri(resolver, mediaUri, null, o)
|
||||||
|
if (o.outMimeType != null) {
|
||||||
|
mediaType = o.outMimeType
|
||||||
|
}
|
||||||
o.inSampleSize = Utils.calculateInSampleSize(o.outWidth, o.outHeight,
|
o.inSampleSize = Utils.calculateInSampleSize(o.outWidth, o.outHeight,
|
||||||
sizeLimit.x, sizeLimit.y)
|
sizeLimit.x, sizeLimit.y)
|
||||||
o.inJustDecodeBounds = false
|
o.inJustDecodeBounds = false
|
||||||
val bitmap = BitmapFactory.decodeStream(st, null, o)
|
val bitmap = BitmapFactoryUtils.decodeUri(resolver, mediaUri, null, o)
|
||||||
val os = DirectByteArrayOutputStream()
|
if (bitmap != null) {
|
||||||
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, os)
|
val os = DirectByteArrayOutputStream()
|
||||||
length = os.size().toLong()
|
when (mediaType) {
|
||||||
cis = ContentLengthInputStream(os.inputStream(true), length)
|
"image/png", "image/x-png", "image/webp", "image-x-webp" -> {
|
||||||
|
bitmap.compress(Bitmap.CompressFormat.PNG, 0, os)
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, os)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
length = os.size().toLong()
|
||||||
|
cis = ContentLengthInputStream(os.inputStream(true), length)
|
||||||
|
} else {
|
||||||
|
val st = resolver.openInputStream(mediaUri) ?: throw FileNotFoundException(mediaUri.toString())
|
||||||
|
length = st.available().toLong()
|
||||||
|
cis = ContentLengthInputStream(st, length)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
val st = resolver.openInputStream(mediaUri) ?: throw FileNotFoundException(mediaUri.toString())
|
||||||
length = st.available().toLong()
|
length = st.available().toLong()
|
||||||
cis = ContentLengthInputStream(st, length)
|
cis = ContentLengthInputStream(st, length)
|
||||||
}
|
}
|
||||||
|
@ -1483,12 +1483,14 @@ public final class Utils implements Constants {
|
|||||||
|
|
||||||
public static String getTwitterErrorMessage(final Context context, final MicroBlogException te) {
|
public static String getTwitterErrorMessage(final Context context, final MicroBlogException te) {
|
||||||
if (te == null) return null;
|
if (te == null) return null;
|
||||||
if (StatusCodeMessageUtils.containsTwitterError(te.getErrorCode()))
|
if (StatusCodeMessageUtils.containsTwitterError(te.getErrorCode())) {
|
||||||
return StatusCodeMessageUtils.getTwitterErrorMessage(context, te.getErrorCode());
|
return StatusCodeMessageUtils.getTwitterErrorMessage(context, te.getErrorCode());
|
||||||
else if (StatusCodeMessageUtils.containsHttpStatus(te.getStatusCode()))
|
} else if (StatusCodeMessageUtils.containsHttpStatus(te.getStatusCode())) {
|
||||||
return StatusCodeMessageUtils.getHttpStatusMessage(context, te.getStatusCode());
|
return StatusCodeMessageUtils.getHttpStatusMessage(context, te.getStatusCode());
|
||||||
else
|
} else if (te.getErrorMessage() != null) {
|
||||||
return te.getMessage();
|
return te.getErrorMessage();
|
||||||
|
}
|
||||||
|
return te.getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -367,8 +367,9 @@ class BackgroundOperationService : IntentService("background_operation"), Consta
|
|||||||
var body: FileBody? = null
|
var body: FileBody? = null
|
||||||
try {
|
try {
|
||||||
body = UpdateStatusTask.getBodyFromMedia(contentResolver,
|
body = UpdateStatusTask.getBodyFromMedia(contentResolver,
|
||||||
mediaUri, null, MessageMediaUploadListener(this,
|
mediaUri, null, ParcelableMedia.Type.IMAGE,
|
||||||
notificationManager, builder, text))
|
MessageMediaUploadListener(this, notificationManager,
|
||||||
|
builder, text))
|
||||||
val uploadResp = uploadMedia(twitterUpload, body)
|
val uploadResp = uploadMedia(twitterUpload, body)
|
||||||
val response = twitter.sendDirectMessage(recipientId,
|
val response = twitter.sendDirectMessage(recipientId,
|
||||||
text, uploadResp.id)
|
text, uploadResp.id)
|
||||||
|
@ -0,0 +1,30 @@
|
|||||||
|
package org.mariotaku.twidere.util
|
||||||
|
|
||||||
|
import android.content.ContentResolver
|
||||||
|
import android.graphics.Bitmap
|
||||||
|
import android.graphics.BitmapFactory
|
||||||
|
import android.graphics.Rect
|
||||||
|
import android.net.Uri
|
||||||
|
import java.io.IOException
|
||||||
|
import java.io.InputStream
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by mariotaku on 16/7/31.
|
||||||
|
*/
|
||||||
|
object BitmapFactoryUtils {
|
||||||
|
|
||||||
|
@Throws(IOException::class)
|
||||||
|
fun decodeUri(contentResolver: ContentResolver, uri: Uri, outPadding: Rect?,
|
||||||
|
opts: BitmapFactory.Options?, close: Boolean = true): Bitmap? {
|
||||||
|
var st: InputStream? = null
|
||||||
|
try {
|
||||||
|
st = contentResolver.openInputStream(uri)
|
||||||
|
return BitmapFactory.decodeStream(st, outPadding, opts)
|
||||||
|
} finally {
|
||||||
|
if (close) {
|
||||||
|
Utils.closeSilently(st)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user