Android-从相册中选择图片显示出来
来源:互联网 发布:旋转秋千知乎 编辑:程序博客网 时间:2024/06/05 11:24
一、选择图片
定义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); }}
0 0
- Android-从相册中选择图片显示出来
- Android从相册中选择图片显示出来
- 我的Android心得(6)--从相册中选择图片显示出来
- Android 多媒体应用:从相册选择照片显示(解决相册中图片显示不出来的问题)
- android---从相册中选择图片
- Android 从相册选择图片
- 从相册中选择图片
- android开发,从本地选择图片,并显示出来
- android从系统相册中选择一张图片
- Android 从相册和拍照选择图片
- Android从相册选择图片并裁剪
- Android 拍照、从相册选择图片
- 从相册中选择多张图片
- 从相册选择图片
- android保存图片到本地并可以在相册中显示出来
- android保存图片到本地并可以在相册中显示出来
- android保存图片到本地并可以在相册中显示出来
- android保存图片到本地并可以在相册中显示出来
- TODOList
- C++各容器的语法及特点
- MongoDB——数据库操作
- NodeJS连接自定义地址的Mysql数据库
- Java类变量和成员变量初始化过程
- Android-从相册中选择图片显示出来
- swing获取固定像素长度的字符串
- 添加nexus第三方jar
- mac快捷键 xcode快捷键
- STM32按键程序
- 用svn服务器创建CocoaPods私有库的注意点
- 三次拉格朗日插值多项式
- 自动驾驶3-百度
- 调用系统摄像头和相册选择图片