我的Android心得(6)--从相册中选择图片显示出来

本文分享了如何在Android应用中从相册选择图片并进行缩放显示的步骤,包括启动图库Intent,使用BitmapFactory进行高效缩放,并在ImageView中展示的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、选择图片

定义Intent跳转到特定图库的Uri下挑选,然后将挑选结果返回给Activity

用到startActivityForResult

Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT);
然后在onActivityForResult保存


二、图片缩放

这里讲的很详细

http://blog.csdn.net/lincyang/article/details/6651582

照它所说有3种方法:

第一种是BitmapFactory和BitmapFactory.Options,同比例缩放,但是效率高

第二种是使用Bitmap加Matrix来缩放,能够不同比例,但是效率低

第三种是用2.2新加的类ThumbnailUtils来做,这方法不懂,好复杂。。


我用第一种,将图片放在Imageview中

	private void showPhoto(ImageView photo){
		String picturePath = target.getInfo(Target.TargetPhotoPath);// 图片的uri
		if(picturePath.equals(""))
			return;
		// 缩放图片, width, height 按相同比例缩放图片
        BitmapFactory.Options options = new BitmapFactory.Options();
        // options 设为true时,构造出的bitmap没有图片,只有一些长宽等配置信息,但比较快,设为false时,才有图片
        options.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(picturePath, options);
        int scale = (int)( options.outWidth / (float)300);
        if(scale <= 0)
        	scale = 1;
        options.inSampleSize = scale;
        options.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeFile(picturePath, options);
        
        photo.setImageBitmap(bitmap);
        photo.setMaxHeight(350);
	}
	

三、代码

public class RemindActivity extends Activity {

	private ImageView photo;
	private static final int RESULT = 1;
	Target target;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_remind);
				
		photo = (ImageView)findViewById(R.id.photo);
		target = new Target(this);
		
		showPhoto(photo);
		
		photo.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Dialog dialog = new AlertDialog.Builder(RemindActivity.this)
				.setTitle("从图库里选择照片").setMessage("确定要更换照片?").setPositiveButton("确定", 
						new DialogInterface.OnClickListener() {
							
							@Override
							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub
								Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
								startActivityForResult(intent, RESULT);
							}
						}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
							
							@Override
							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub
								dialog.cancel();
							}
						}).create();
				dialog.show();
			}
			
		});
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == RESULT && resultCode == RESULT_OK && data != null){
			
			Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
 
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();
 
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
 
            target.setInfo(Target.TargetPhotoPath, picturePath);
            
            showPhoto(photo);
		}
	}
	
	private void showPhoto(ImageView photo){
		String picturePath = target.getInfo(Target.TargetPhotoPath);
		if(picturePath.equals(""))
			return;
		// 缩放图片, width, height 按相同比例缩放图片
        BitmapFactory.Options options = new BitmapFactory.Options();
        // options 设为true时,构造出的bitmap没有图片,只有一些长宽等配置信息,但比较快,设为false时,才有图片
        options.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(picturePath, options);
        int scale = (int)( options.outWidth / (float)300);
        if(scale <= 0)
        	scale = 1;
        options.inSampleSize = scale;
        options.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeFile(picturePath, options);
        
        photo.setImageBitmap(bitmap);
        photo.setMaxHeight(350);
	}
}




评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值