Android中实现从SD卡的Gallery画廊带回图片

   

 在App中我们常常遇到通过点击某个按钮,就会跳转到图片库页面,也就是Gallery画廊,那么一方面我们要如何从其中获取我们想要的图片呢?并且带回我们正在浏览的页面。另一方面,我们还有控制图片的大小,如何让带回的图片自适应我们正在浏览页面框的大小?接下来我就为大家揭晓答案

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>

先建一个可以点击的按钮和一个放图片的ImageView控件,接下来完成代码部分

ImageDemoActivity.java

import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class ImageDemoActivity extends Activity {
    /** Called when the activity is first created. */
	private Button btn1;
	private ImageView imageView1;
	int hd;
	int wd;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn1=  (Button) findViewById(R.id.button1);
        imageView1=  (ImageView) findViewById(R.id.imageView1);
        btn1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				//用Intent调用系统图库
				Intent i=new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
				startActivityForResult(i, 0);
			}
		});
        
    }
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode==RESULT_OK&&requestCode==0) {
			Uri imageUri=data.getData();
			Display display=getWindowManager().getDefaultDisplay();//获取设备尺寸
			hd=display.getHeight();
			wd=display.getWidth();
			BitmapFactory.Options bo=new BitmapFactory.Options();//只加载图片的尺寸
			bo.inJustDecodeBounds=true;//设置为true,并不会返回一个Bitmap图像
			try {
				Bitmap bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri), null, bo);
				//通过getContentResolver().openInputStream(imageUri)将URI转换成流
				int heightRatio=(int) Math.ceil(bo.outHeight/hd);
				int widthRatio=(int) Math.ceil(bo.outWidth/wd);
				if (heightRatio>widthRatio) {
					bo.inSampleSize=heightRatio;
				}else {
					bo.inSampleSize=widthRatio;//inSampleSize成员变量,可以把图像的实际宽高和期望的比较得到合理的值
				}
				//以上代码只是设置图片尺寸,这个时候我要整个图片加载并显示
				bo.inJustDecodeBounds=false;
				bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri), null, bo);
				//根据第一次得到的合适的缩放比例生成缩络图
				imageView1.setImageBitmap(bitmap);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}
首先我们是通过Intent调用系统图库
Intent i=new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

为了让图片能够符合我们的要求,这里用到了startActivityForResult()方法来带回图片,在完成方法startActivityForResult()之后我们要覆写onActivityResult()方法,此方法能够帮我们传递回调的数据
为了让图片的大小能够符合我们显示器的大小,这里做了一些比较通过获取设备的尺寸BitmapFactory.Options bo=new BitmapFactory.Options();,其他的都在代码中做了详细的注释,就不一一介绍了,对了,要注意bo.inJustDecodeBounds=true;在代码中的变化。







猜你喜欢

转载自blog.csdn.net/mxcsdn/article/details/51136620
今日推荐