FileDocCategorySizeDatePackage
UriImage.javaAPI DocAndroid 1.5 API7543Wed May 06 22:42:46 BST 2009com.android.mms.ui

UriImage

public class UriImage extends Object

Fields Summary
private static final String
TAG
private static final boolean
DEBUG
private static final boolean
LOCAL_LOGV
private final android.content.Context
mContext
private final android.net.Uri
mUri
private String
mContentType
private String
mPath
private String
mSrc
private int
mWidth
private int
mHeight
Constructors Summary
public UriImage(android.content.Context context, android.net.Uri uri)


         
        if ((null == context) || (null == uri)) {
            throw new IllegalArgumentException();
        }

        String scheme = uri.getScheme();
        if (scheme.equals("content")) {
            initFromContentUri(context, uri);
        } else if (uri.getScheme().equals("file")) {
            initFromFile(context, uri);
        }
        
        mSrc = mPath.substring(mPath.lastIndexOf('/") + 1);
        
        // Some MMSCs appear to have problems with filenames
        // containing a space.  So just replace them with
        // underscores in the name, which is typically not
        // visible to the user anyway.
        mSrc = mSrc.replace(' ", '_");

        mContext = context;
        mUri = uri;

        decodeBoundsInfo();
    
Methods Summary
private voiddecodeBoundsInfo()

        InputStream input = null;
        try {
            input = mContext.getContentResolver().openInputStream(mUri);
            BitmapFactory.Options opt = new BitmapFactory.Options();
            opt.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(input, null, opt);
            mWidth = opt.outWidth;
            mHeight = opt.outHeight;
        } catch (FileNotFoundException e) {
            // Ignore
            Log.e(TAG, "IOException caught while opening stream", e);
        } finally {
            if (null != input) {
                try {
                    input.close();
                } catch (IOException e) {
                    // Ignore
                    Log.e(TAG, "IOException caught while closing stream", e);
                }
            }
        }
    
public java.lang.StringgetContentType()

        return mContentType;
    
public intgetHeight()

        return mHeight;
    
public com.google.android.mms.pdu.PduPartgetResizedImageAsPart(int widthLimit, int heightLimit)

        PduPart part = new PduPart();

        byte[] data = getResizedImageData(widthLimit, heightLimit);
        if (data == null) {
            if (LOCAL_LOGV) {
                Log.v(TAG, "Resize image failed.");
            }
            return null;
        }

        part.setData(data);
        part.setContentType(getContentType().getBytes());
        String src = getSrc();
        byte[] srcBytes = src.getBytes();
        part.setContentLocation(srcBytes);
        part.setFilename(srcBytes);
        part.setContentId(src.substring(0, src.lastIndexOf(".")).getBytes());

        return part;
    
private byte[]getResizedImageData(int widthLimit, int heightLimit)

        int outWidth = mWidth;
        int outHeight = mHeight;

        int s = 1;
        while ((outWidth / s > widthLimit) || (outHeight / s > heightLimit)) {
            s *= 2;
        }
        if (LOCAL_LOGV) {
            Log.v(TAG, "outWidth=" + outWidth / s
                    + " outHeight=" + outHeight / s);
        }
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = s;

        InputStream input = null;
        try {
            input = mContext.getContentResolver().openInputStream(mUri);
            Bitmap b = BitmapFactory.decodeStream(input, null, options);
            if (b == null) {
                return null;
            }

            ByteArrayOutputStream os = new ByteArrayOutputStream();
            b.compress(CompressFormat.JPEG, MessageUtils.IMAGE_COMPRESSION_QUALITY, os);
            return os.toByteArray();
        } catch (FileNotFoundException e) {
            Log.e(TAG, e.getMessage(), e);
            return null;
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    Log.e(TAG, e.getMessage(), e);
                }
            }
        }
    
public java.lang.StringgetSrc()

        return mSrc;
    
public intgetWidth()

        return mWidth;
    
private voidinitFromContentUri(android.content.Context context, android.net.Uri uri)

        Cursor c = SqliteWrapper.query(context, context.getContentResolver(),
                            uri, null, null, null, null);

        if (c == null) {
            throw new IllegalArgumentException(
                    "Query on " + uri + " returns null result.");
        }

        try {
            if ((c.getCount() != 1) || !c.moveToFirst()) {
                throw new IllegalArgumentException(
                        "Query on " + uri + " returns 0 or multiple rows.");
            }

            String filePath;
            if (ImageModel.isMmsUri(uri)) {
                filePath = c.getString(c.getColumnIndexOrThrow(Part.FILENAME));
                if (TextUtils.isEmpty(filePath)) {
                    filePath = c.getString(
                            c.getColumnIndexOrThrow(Part._DATA));
                }
                mContentType = c.getString(
                        c.getColumnIndexOrThrow(Part.CONTENT_TYPE));
            } else {
                filePath = c.getString(
                        c.getColumnIndexOrThrow(Images.Media.DATA));
                mContentType = c.getString(
                        c.getColumnIndexOrThrow(Images.Media.MIME_TYPE));
            }
            mPath = filePath;
        } finally {
            c.close();
        }
    
private voidinitFromFile(android.content.Context context, android.net.Uri uri)

        MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
        String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
        mContentType = mimeTypeMap.getMimeTypeFromExtension(extension);
        if (mContentType == null) {
            throw new IllegalArgumentException(
                    "Unable to determine extension for " + uri.toString());
        }
        mPath = uri.getPath();