Search in sources :

Example 1 with ExceedMessageSizeException

use of com.moez.QKSMS.ExceedMessageSizeException in project qksms by moezbhatti.

the class ImageModel method resizeMedia.

@Override
protected void resizeMedia(int byteLimit, long messageId) throws MmsException {
    UriImage image = new UriImage(mContext, getUri());
    int widthLimit = MmsConfig.getMaxImageWidth();
    int heightLimit = MmsConfig.getMaxImageHeight();
    int size = getMediaSize();
    // possible.
    if (image.getHeight() > image.getWidth()) {
        int temp = widthLimit;
        widthLimit = heightLimit;
        heightLimit = temp;
    }
    if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
        Log.v(TAG, "resizeMedia size: " + size + " image.getWidth(): " + image.getWidth() + " widthLimit: " + widthLimit + " image.getHeight(): " + image.getHeight() + " heightLimit: " + heightLimit + " image.getContentType(): " + image.getContentType());
    }
    // set the size.
    if (size != 0 && size <= byteLimit && image.getWidth() <= widthLimit && image.getHeight() <= heightLimit && SUPPORTED_MMS_IMAGE_CONTENT_TYPES.contains(image.getContentType())) {
        if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
            Log.v(TAG, "resizeMedia - already sized");
        }
        return;
    }
    PduPart part = image.getResizedImageAsPart(widthLimit, heightLimit, byteLimit);
    if (part == null) {
        throw new ExceedMessageSizeException("Not enough memory to turn image into part: " + getUri());
    }
    // Update the content type because it may have changed due to resizing/recompressing
    mContentType = new String(part.getContentType());
    String src = getSrc();
    byte[] srcBytes = src.getBytes();
    part.setContentLocation(srcBytes);
    int period = src.lastIndexOf(".");
    byte[] contentId = period != -1 ? src.substring(0, period).getBytes() : srcBytes;
    part.setContentId(contentId);
    PduPersister persister = PduPersister.getPduPersister(mContext);
    this.mSize = part.getData().length;
    if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
        Log.v(TAG, "resizeMedia mSize: " + mSize);
    }
    Uri newUri = persister.persistPart(part, messageId, null);
    setUri(newUri);
}
Also used : UriImage(com.moez.QKSMS.common.google.UriImage) PduPersister(com.google.android.mms.pdu_alt.PduPersister) PduPart(com.google.android.mms.pdu_alt.PduPart) ExceedMessageSizeException(com.moez.QKSMS.ExceedMessageSizeException) Uri(android.net.Uri)

Example 2 with ExceedMessageSizeException

use of com.moez.QKSMS.ExceedMessageSizeException in project qksms by moezbhatti.

the class SlideshowModel method finalResize.

/**
 * Resize all the resizeable media objects to fit in the remaining size of the slideshow.
 * This should be called off of the UI thread.
 *
 * @throws com.google.android.mms.MmsException, ExceedMessageSizeException
 */
public void finalResize(Uri messageUri) throws MmsException, ExceedMessageSizeException {
    // Figure out if we have any media items that need to be resized and total up the
    // sizes of the items that can't be resized.
    int resizableCnt = 0;
    int fixedSizeTotal = 0;
    for (SlideModel slide : mSlides) {
        for (MediaModel media : slide) {
            if (media.getMediaResizable()) {
                ++resizableCnt;
            } else {
                fixedSizeTotal += media.getMediaSize();
            }
        }
    }
    if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
        Log.v(TAG, "finalResize: original message size: " + getCurrentMessageSize() + " getMaxMessageSize: " + MmsConfig.getMaxMessageSize() + " fixedSizeTotal: " + fixedSizeTotal);
    }
    if (resizableCnt > 0) {
        int remainingSize = MmsConfig.getMaxMessageSize() - fixedSizeTotal - SLIDESHOW_SLOP;
        if (remainingSize <= 0) {
            throw new ExceedMessageSizeException("No room for pictures");
        }
        long messageId = ContentUris.parseId(messageUri);
        int bytesPerMediaItem = remainingSize / resizableCnt;
        // Resize the resizable media items to fit within their byte limit.
        for (SlideModel slide : mSlides) {
            for (MediaModel media : slide) {
                if (media.getMediaResizable()) {
                    media.resizeMedia(bytesPerMediaItem, messageId);
                }
            }
        }
        // One last time through to calc the real message size.
        int totalSize = 0;
        for (SlideModel slide : mSlides) {
            for (MediaModel media : slide) {
                totalSize += media.getMediaSize();
            }
        }
        if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
            Log.v(TAG, "finalResize: new message size: " + totalSize);
        }
        if (totalSize > MmsConfig.getMaxMessageSize()) {
            throw new ExceedMessageSizeException("After compressing pictures, message too big");
        }
        setCurrentMessageSize(totalSize);
        // clear the cached pdu body
        onModelChanged(this, true);
        PduBody pb = toPduBody();
        // This will write out all the new parts to:
        // /data/data/com.android.providers.telephony/app_parts
        // and at the same time delete the old parts.
        PduPersister.getPduPersister(mContext).updateParts(messageUri, pb, null);
    }
}
Also used : PduBody(com.google.android.mms.pdu_alt.PduBody) ExceedMessageSizeException(com.moez.QKSMS.ExceedMessageSizeException)

Aggregations

ExceedMessageSizeException (com.moez.QKSMS.ExceedMessageSizeException)2 Uri (android.net.Uri)1 PduBody (com.google.android.mms.pdu_alt.PduBody)1 PduPart (com.google.android.mms.pdu_alt.PduPart)1 PduPersister (com.google.android.mms.pdu_alt.PduPersister)1 UriImage (com.moez.QKSMS.common.google.UriImage)1