ImageSpan.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17package android.text.style;
18
19import android.content.Context;
20import android.graphics.Bitmap;
21import android.graphics.BitmapFactory;
22import android.graphics.drawable.BitmapDrawable;
23import android.graphics.drawable.Drawable;
24import android.net.Uri;
25import android.util.Log;
26
27import java.io.InputStream;
28
29public class ImageSpan extends DynamicDrawableSpan {
30    private Drawable mDrawable;
31    private Uri mContentUri;
32    private int mResourceId;
33    private Context mContext;
34    private String mSource;
35
36
37    public ImageSpan(Bitmap b) {
38        mDrawable = new BitmapDrawable(b);
39        mDrawable.setBounds(0, 0, mDrawable.getIntrinsicWidth(),
40                mDrawable.getIntrinsicHeight());
41    }
42
43    public ImageSpan(Drawable d) {
44        mDrawable = d;
45    }
46
47    public ImageSpan(Drawable d, String source) {
48        mDrawable = d;
49        mSource = source;
50    }
51
52    public ImageSpan(Context context, Uri uri) {
53        mContext = context;
54        mContentUri = uri;
55    }
56
57    public ImageSpan(Context context, int resourceId) {
58        mContext = context;
59        mResourceId = resourceId;
60    }
61
62    @Override
63    public Drawable getDrawable() {
64        Drawable drawable = null;
65
66        if (mDrawable != null) {
67            drawable = mDrawable;
68        } else  if (mContentUri != null) {
69            Bitmap bitmap = null;
70            try {
71                InputStream is = mContext.getContentResolver().openInputStream(
72                        mContentUri);
73                bitmap = BitmapFactory.decodeStream(is);
74                drawable = new BitmapDrawable(bitmap);
75                is.close();
76            } catch (Exception e) {
77                Log.e("sms", "Failed to loaded content " + mContentUri, e);
78            }
79        } else {
80            try {
81                drawable = mContext.getResources().getDrawable(mResourceId);
82                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
83                        drawable.getIntrinsicHeight());
84            } catch (Exception e) {
85                Log.e("sms", "Unable to find resource: " + mResourceId);
86            }
87        }
88
89        return drawable;
90    }
91
92    /**
93     * Returns the source string that was saved during construction.
94     */
95    public String getSource() {
96        return mSource;
97    }
98
99}
100