Chinaunix首页 | 论坛 | 博客
  • 博客访问: 477472
  • 博文数量: 155
  • 博客积分: 2954
  • 博客等级: 少校
  • 技术积分: 1000
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-12 22:00
文章分类

全部博文(155)

文章存档

2014年(2)

2013年(5)

2012年(10)

2011年(33)

2010年(105)

我的朋友

分类: 嵌入式

2010-10-21 10:31:08

与上篇相比,读取相册图片会稍微麻烦些,因为要将读取到问图片解析成Bitmap的格式,然后通过ImageView标签进行显示。

下面来看看关键代码

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;

public class FindImage extends Activity {
    /** Called when the activity is first created. */

    public static final String MIME_TYPE_IMAGE_JPEG = "image/jpeg";
    public static final int ACTIVITY_GET_IMAGE = 0;
    public static final String KEY_FILE_NAME = "name";
    public static final String KEY_FILE_TYPE = "type";
    public static final String KEY_FILE_BITS = "bits";
    public static final String KEY_FILE_OVERWRITE = "overwrite";
    public static final String KEY_FILE_URL = "url";
    private byte[] mContent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
        getImage.addCategory(Intent.CATEGORY_OPENABLE);
        getImage.setType(MIME_TYPE_IMAGE_JPEG);
        startActivityForResult(getImage, ACTIVITY_GET_IMAGE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // newImage(data.getData());

        if (resultCode != RESULT_OK) {

            return;

        }

        Bitmap bm = null;

        ContentResolver resolver = getContentResolver();

        if (requestCode == ACTIVITY_GET_IMAGE) {

            try {
                //获得图片的uri
                Uri originalUri = data.getData();
                //将图片内容解析成字节数组
                mContent = getBytesFromInputStream(resolver.openInputStream(Uri
                        .parse(originalUri.toString())), 3500000);
                //将字节数组转换为ImageView可调用的Bitmap对象
                bm = getPicFromBytes(mContent, null);
                //显示图片
                ImageView iv = getThemedImageView();
                iv.setImageBitmap(bm);
                setContentView(iv);
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }

        }

    }

    public static Bitmap getPicFromBytes(byte[] bytes,
            BitmapFactory.Options opts) {

        if (bytes != null)
            if (opts != null)
                return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,
                        opts);
            else
                return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
        return null;
    }

    public static byte[] getBytesFromInputStream(InputStream is, int bufsiz)
            throws IOException {
        int total = 0;
        byte[] bytes = new byte[4096];
        ByteBuffer bb = ByteBuffer.allocate(bufsiz);

        while (true) {
            int read = is.read(bytes);
            if (read == -1)
                break;
            bb.put(bytes, 0, read);
            total += read;
        }

        byte[] content = new byte[total];
        bb.flip();
        bb.get(content, 0, total);

        return content;
    }

    private ImageView getThemedImageView() {
        ImageView iv = new ImageView(this);
        iv.setBackgroundResource(android.R.drawable.gallery_thumb);
        return iv;
    }
}

 

运行效果如下:

image

 

选择完随即显示。

image

阅读(5743) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~