Android从相册中选择图片显示出来

下面的两篇博客我是选择其中的一部分使用的。大家可以自己试试。

第一篇:http://blog.csdn.net/jackyguo1992/article/details/26729107

一、选择图片

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

用到startActivityForResult

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);  
  2. startActivityForResult(intent, RESULT);  
然后在onActivityForResult保存


二、图片缩放

这里讲的很详细

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

照它所说有3种方法:

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

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

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


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

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. private void showPhoto(ImageView photo){  
  2.     String picturePath = target.getInfo(Target.TargetPhotoPath);// 图片的uri  
  3.     if(picturePath.equals(""))  
  4.         return;  
  5.     // 缩放图片, width, height 按相同比例缩放图片  
  6.        BitmapFactory.Options options = new BitmapFactory.Options();  
  7.        // options 设为true时,构造出的bitmap没有图片,只有一些长宽等配置信息,但比较快,设为false时,才有图片  
  8.        options.inJustDecodeBounds = true;  
  9.        Bitmap bitmap = BitmapFactory.decodeFile(picturePath, options);  
  10.        int scale = (int)( options.outWidth / (float)300);  
  11.        if(scale <= 0)  
  12.         scale = 1;  
  13.        options.inSampleSize = scale;  
  14.        options.inJustDecodeBounds = false;  
  15.        bitmap = BitmapFactory.decodeFile(picturePath, options);  
  16.          
  17.        photo.setImageBitmap(bitmap);  
  18.        photo.setMaxHeight(350);  
  19. }  

三、代码

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class RemindActivity extends Activity {  
  2.   
  3.     private ImageView photo;  
  4.     private static final int RESULT = 1;  
  5.     Target target;  
  6.       
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_remind);  
  11.                   
  12.         photo = (ImageView)findViewById(R.id.photo);  
  13.         target = new Target(this);  
  14.           
  15.         showPhoto(photo);  
  16.           
  17.         photo.setOnClickListener(new OnClickListener(){  
  18.   
  19.             @Override  
  20.             public void onClick(View arg0) {  
  21.                 // TODO Auto-generated method stub  
  22.                 Dialog dialog = new AlertDialog.Builder(RemindActivity.this)  
  23.                 .setTitle("从图库里选择照片").setMessage("确定要更换照片?").setPositiveButton("确定",   
  24.                         new DialogInterface.OnClickListener() {  
  25.                               
  26.                             @Override  
  27.                             public void onClick(DialogInterface dialog, int which) {  
  28.                                 // TODO Auto-generated method stub  
  29.                                 Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);  
  30.                                 startActivityForResult(intent, RESULT);  
  31.                             }  
  32.                         }).setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  33.                               
  34.                             @Override  
  35.                             public void onClick(DialogInterface dialog, int which) {  
  36.                                 // TODO Auto-generated method stub  
  37.                                 dialog.cancel();  
  38.                             }  
  39.                         }).create();  
  40.                 dialog.show();  
  41.             }  
  42.               
  43.         });  
  44.           
  45.     }  
  46.       
  47.     @Override  
  48.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  49.         // TODO Auto-generated method stub  
  50.         super.onActivityResult(requestCode, resultCode, data);  
  51.         if(requestCode == RESULT && resultCode == RESULT_OK && data != null){  
  52.               
  53.             Uri selectedImage = data.getData();  
  54.             String[] filePathColumn = { MediaStore.Images.Media.DATA };  
  55.    
  56.             Cursor cursor = getContentResolver().query(selectedImage,  
  57.                     filePathColumn, nullnullnull);  
  58.             cursor.moveToFirst();  
  59.    
  60.             int columnIndex = cursor.getColumnIndex(filePathColumn[0]);  
  61.             String picturePath = cursor.getString(columnIndex);  
  62.             cursor.close();  
  63.    
  64.             target.setInfo(Target.TargetPhotoPath, picturePath);  
  65.               
  66.             showPhoto(photo);  
  67.         }  
  68.     }  
  69.       
  70.     private void showPhoto(ImageView photo){  
  71.         String picturePath = target.getInfo(Target.TargetPhotoPath);  
  72.         if(picturePath.equals(""))  
  73.             return;  
  74.         // 缩放图片, width, height 按相同比例缩放图片  
  75.         BitmapFactory.Options options = new BitmapFactory.Options();  
  76.         // options 设为true时,构造出的bitmap没有图片,只有一些长宽等配置信息,但比较快,设为false时,才有图片  
  77.         options.inJustDecodeBounds = true;  
  78.         Bitmap bitmap = BitmapFactory.decodeFile(picturePath, options);  
  79.         int scale = (int)( options.outWidth / (float)300);  
  80.         if(scale <= 0)  
  81.             scale = 1;  
  82.         options.inSampleSize = scale;  
  83.         options.inJustDecodeBounds = false;  
  84.         bitmap = BitmapFactory.decodeFile(picturePath, options);  
  85.           
  86.         photo.setImageBitmap(bitmap);  
  87.         photo.setMaxHeight(350);  
  88.     }  

第二篇地址:http://www.oschina.net/question/234345_40138

public class Lesson_01_Pic extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button button = (Button)findViewById(R.id.b01);
        button.setText("选择图片");
        button.setOnClickListener(new Button.OnClickListener(){
			@Override
			public void onClick(View v) {
			 	Intent intent = new Intent();
		        /* 开启Pictures画面Type设定为image */
		        intent.setType("image/*");
		        /* 使用Intent.ACTION_GET_CONTENT这个Action */
		        intent.setAction(Intent.ACTION_GET_CONTENT); 
		        /* 取得相片后返回本画面 */
		        startActivityForResult(intent, 1);
			}
        	
        });
    }
    
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			Uri uri = data.getData();
			Log.e("uri", uri.toString());
			ContentResolver cr = this.getContentResolver();
			try {
				Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
				ImageView imageView = (ImageView) findViewById(R.id.iv01);
				/* 将Bitmap设定到ImageView */
				imageView.setImageBitmap(bitmap);
			} catch (FileNotFoundException e) {
				Log.e("Exception", e.getMessage(),e);
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值