您的位置:首页 > 移动开发 > Android开发

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

2016-11-28 13:42 267 查看

一、选择图片

定义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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: