Search in sources :

Example 1 with WPEditImageSpan

use of org.wordpress.android.editor.legacy.WPEditImageSpan in project WordPress-Android by wordpress-mobile.

the class LegacyEditorFragment method loadWPImageSpanThumbnail.

private void loadWPImageSpanThumbnail(MediaFile mediaFile, String imageURL, ImageLoader imageLoader) {
    if (mediaFile == null || imageURL == null) {
        return;
    }
    final String mediaId = mediaFile.getMediaId();
    if (mediaId == null) {
        return;
    }
    final int maxThumbWidth = ImageUtils.getMaximumThumbnailWidthForEditor(getActivity());
    imageLoader.get(imageURL, new ImageLoader.ImageListener() {

        @Override
        public void onErrorResponse(VolleyError arg0) {
        }

        @Override
        public void onResponse(ImageLoader.ImageContainer container, boolean arg1) {
            Bitmap downloadedBitmap = container.getBitmap();
            if (downloadedBitmap == null) {
                // no bitmap downloaded from the server.
                return;
            }
            if (downloadedBitmap.getWidth() < MIN_THUMBNAIL_WIDTH) {
                // Picture is too small. Show the placeholder in this case.
                return;
            }
            Bitmap resizedBitmap;
            // resize the downloaded bitmap
            resizedBitmap = ImageUtils.getScaledBitmapAtLongestSide(downloadedBitmap, maxThumbWidth);
            if (resizedBitmap == null) {
                return;
            }
            final EditText editText = mContentEditText;
            Editable s = editText.getText();
            if (s == null) {
                return;
            }
            WPImageSpan[] spans = s.getSpans(0, s.length(), WPImageSpan.class);
            if (spans.length != 0 && getActivity() != null) {
                for (WPImageSpan is : spans) {
                    MediaFile mediaFile = is.getMediaFile();
                    if (mediaFile == null) {
                        continue;
                    }
                    if (mediaId.equals(mediaFile.getMediaId()) && !is.isNetworkImageLoaded()) {
                        // replace the existing span with a new one with the correct image, re-add
                        // it to the same position.
                        int spanStart = is.getStartPosition();
                        int spanEnd = is.getEndPosition();
                        WPEditImageSpan imageSpan = new WPEditImageSpan(getActivity(), resizedBitmap, is.getImageSource());
                        imageSpan.setMediaFile(is.getMediaFile());
                        imageSpan.setNetworkImageLoaded(true);
                        imageSpan.setPosition(spanStart, spanEnd);
                        s.removeSpan(is);
                        s.setSpan(imageSpan, spanStart, spanEnd + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        break;
                    }
                }
            }
        }
    }, 0, 0);
}
Also used : VolleyError(com.android.volley.VolleyError) WPEditText(org.wordpress.android.util.widgets.WPEditText) EditText(android.widget.EditText) MediaFile(org.wordpress.android.util.helpers.MediaFile) WPEditImageSpan(org.wordpress.android.editor.legacy.WPEditImageSpan) WPImageSpan(org.wordpress.android.util.helpers.WPImageSpan) Bitmap(android.graphics.Bitmap) Editable(android.text.Editable) ImageLoader(com.android.volley.toolbox.ImageLoader)

Example 2 with WPEditImageSpan

use of org.wordpress.android.editor.legacy.WPEditImageSpan in project WordPress-Android by wordpress-mobile.

the class LegacyEditorFragment method createWPEditImageSpanLocal.

private WPEditImageSpan createWPEditImageSpanLocal(Context context, MediaFile mediaFile) {
    if (context == null || mediaFile == null || mediaFile.getFilePath() == null) {
        return null;
    }
    Uri imageUri = Uri.parse(mediaFile.getFilePath());
    Bitmap thumbnailBitmap;
    if (MediaUtils.isVideo(imageUri.toString())) {
        thumbnailBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.media_movieclip);
    } else {
        thumbnailBitmap = ImageUtils.getWPImageSpanThumbnailFromFilePath(context, imageUri.getEncodedPath(), ImageUtils.getMaximumThumbnailWidthForEditor(context));
        if (thumbnailBitmap == null) {
            // Use a placeholder in case thumbnail can't be decoded (OOM for instance)
            thumbnailBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.legacy_dashicon_format_image_big_grey);
        }
    }
    WPEditImageSpan imageSpan = new WPEditImageSpan(context, thumbnailBitmap, imageUri);
    mediaFile.setWidth(MediaUtils.getMaximumImageWidth(context, imageUri, mBlogSettingMaxImageWidth));
    imageSpan.setMediaFile(mediaFile);
    return imageSpan;
}
Also used : Bitmap(android.graphics.Bitmap) WPEditImageSpan(org.wordpress.android.editor.legacy.WPEditImageSpan) Uri(android.net.Uri)

Example 3 with WPEditImageSpan

use of org.wordpress.android.editor.legacy.WPEditImageSpan in project WordPress-Android by wordpress-mobile.

the class LegacyEditorFragment method createWPEditImageSpanRemote.

private WPEditImageSpan createWPEditImageSpanRemote(Context context, MediaFile mediaFile) {
    if (context == null || mediaFile == null || mediaFile.getFileURL() == null) {
        return null;
    }
    int drawable = mediaFile.isVideo() ? R.drawable.media_movieclip : R.drawable.legacy_dashicon_format_image_big_grey;
    Uri uri = Uri.parse(mediaFile.getFileURL());
    WPEditImageSpan imageSpan = new WPEditImageSpan(context, drawable, uri);
    imageSpan.setMediaFile(mediaFile);
    return imageSpan;
}
Also used : WPEditImageSpan(org.wordpress.android.editor.legacy.WPEditImageSpan) Uri(android.net.Uri)

Aggregations

WPEditImageSpan (org.wordpress.android.editor.legacy.WPEditImageSpan)3 Bitmap (android.graphics.Bitmap)2 Uri (android.net.Uri)2 Editable (android.text.Editable)1 EditText (android.widget.EditText)1 VolleyError (com.android.volley.VolleyError)1 ImageLoader (com.android.volley.toolbox.ImageLoader)1 MediaFile (org.wordpress.android.util.helpers.MediaFile)1 WPImageSpan (org.wordpress.android.util.helpers.WPImageSpan)1 WPEditText (org.wordpress.android.util.widgets.WPEditText)1