2 * Copyright (C) 2006 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package android.text.style;
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.graphics.BitmapFactory;
22 import android.graphics.drawable.BitmapDrawable;
23 import android.graphics.drawable.Drawable;
24 import android.net.Uri;
25 import android.util.Log;
27 import java.io.InputStream;
29 public class ImageSpan extends DynamicDrawableSpan {
30 private Drawable mDrawable;
31 private Uri mContentUri;
32 private int mResourceId;
33 private Context mContext;
34 private String mSource;
37 * @deprecated Use {@link #ImageSpan(Context, Bitmap)} instead.
40 public ImageSpan(Bitmap b) {
41 this(null, b, ALIGN_BOTTOM);
45 * @deprecated Use {@link #ImageSpan(Context, Bitmap, int) instead.
48 public ImageSpan(Bitmap b, int verticalAlignment) {
49 this(null, b, verticalAlignment);
52 public ImageSpan(Context context, Bitmap b) {
53 this(context, b, ALIGN_BOTTOM);
57 * @param verticalAlignment one of {@link DynamicDrawableSpan#ALIGN_BOTTOM} or
58 * {@link DynamicDrawableSpan#ALIGN_BASELINE}.
60 public ImageSpan(Context context, Bitmap b, int verticalAlignment) {
61 super(verticalAlignment);
63 mDrawable = context != null
64 ? new BitmapDrawable(context.getResources(), b)
65 : new BitmapDrawable(b);
66 int width = mDrawable.getIntrinsicWidth();
67 int height = mDrawable.getIntrinsicHeight();
68 mDrawable.setBounds(0, 0, width > 0 ? width : 0, height > 0 ? height : 0);
71 public ImageSpan(Drawable d) {
72 this(d, ALIGN_BOTTOM);
76 * @param verticalAlignment one of {@link DynamicDrawableSpan#ALIGN_BOTTOM} or
77 * {@link DynamicDrawableSpan#ALIGN_BASELINE}.
79 public ImageSpan(Drawable d, int verticalAlignment) {
80 super(verticalAlignment);
84 public ImageSpan(Drawable d, String source) {
85 this(d, source, ALIGN_BOTTOM);
89 * @param verticalAlignment one of {@link DynamicDrawableSpan#ALIGN_BOTTOM} or
90 * {@link DynamicDrawableSpan#ALIGN_BASELINE}.
92 public ImageSpan(Drawable d, String source, int verticalAlignment) {
93 super(verticalAlignment);
98 public ImageSpan(Context context, Uri uri) {
99 this(context, uri, ALIGN_BOTTOM);
103 * @param verticalAlignment one of {@link DynamicDrawableSpan#ALIGN_BOTTOM} or
104 * {@link DynamicDrawableSpan#ALIGN_BASELINE}.
106 public ImageSpan(Context context, Uri uri, int verticalAlignment) {
107 super(verticalAlignment);
110 mSource = uri.toString();
113 public ImageSpan(Context context, int resourceId) {
114 this(context, resourceId, ALIGN_BOTTOM);
118 * @param verticalAlignment one of {@link DynamicDrawableSpan#ALIGN_BOTTOM} or
119 * {@link DynamicDrawableSpan#ALIGN_BASELINE}.
121 public ImageSpan(Context context, int resourceId, int verticalAlignment) {
122 super(verticalAlignment);
124 mResourceId = resourceId;
128 public Drawable getDrawable() {
129 Drawable drawable = null;
131 if (mDrawable != null) {
132 drawable = mDrawable;
133 } else if (mContentUri != null) {
134 Bitmap bitmap = null;
136 InputStream is = mContext.getContentResolver().openInputStream(
138 bitmap = BitmapFactory.decodeStream(is);
139 drawable = new BitmapDrawable(mContext.getResources(), bitmap);
140 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
141 drawable.getIntrinsicHeight());
143 } catch (Exception e) {
144 Log.e("sms", "Failed to loaded content " + mContentUri, e);
148 drawable = mContext.getResources().getDrawable(mResourceId);
149 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
150 drawable.getIntrinsicHeight());
151 } catch (Exception e) {
152 Log.e("sms", "Unable to find resource: " + mResourceId);
160 * Returns the source string that was saved during construction.
162 public String getSource() {