picture = (ImageView)findViewById(R.id.picture);
//设置imageView的图片监听器
picture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectPic();
}
});
//打开本地相册选择图片
private void selectPic(){
Intent intent=new Intent();
intent.setType("image/*");
//action表示intent的类型,可以是查看、删除、发布或其他情况;我们选择ACTION_GET_CONTENT,系统可以根据Type类型来调用系统程序选择Type
//类型的内容给你选择
intent.setAction(Intent.ACTION_GET_CONTENT);
//如果第二个参数大于或等于0,那么当用户操作完成后会返回到本程序的onActivityResult方法
startActivityForResult(intent, 1);
}
//把用户选择的图片显示在imageview中
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//用户操作完成,结果码返回是-1,即RESULT_OK
if(resultCode==RESULT_OK){
//获取选中文件的定位符
Uri uri = data.getData();
Log.e("uri", uri.toString());
//使用content的接口
ContentResolver cr = this.getContentResolver();
try {
//获取图片
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
erweima.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
Log.e("Exception", e.getMessage(),e);
}
}else{
//操作错误或没有选择图片
Log.i("MainActivtiy", "operation error");
}
super.onActivityResult(requestCode, resultCode, data);
}
ImageView读取手机图片
猜你喜欢
转载自blog.csdn.net/qq_38367681/article/details/103996798
今日推荐
周排行