80,488
社区成员
发帖
与我相关
我的任务
分享
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.test);
tv1 = (TextView) findViewById(R.id.one);
tv2 = (TextView) findViewById(R.id.two);
setuser_headimg = (ImageView)findViewById(R.id.image);//头像
Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT); //"android.intent.action.GET_CONTENT"
String IMAGE_UNSPECIFIED = "image/*";
innerIntent.addCategory(Intent.CATEGORY_OPENABLE);
innerIntent.setType(IMAGE_UNSPECIFIED); //查看类型 String IMAGE_UNSPECIFIED = "image/*"; 详细的类型在 com.google.android.mms.ContentType 中
Intent wrapperIntent = Intent.createChooser(innerIntent, null);
int requestCode = 1;
startActivityForResult(wrapperIntent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
Log.i("requestCode", "requestCode"+requestCode+" resultCode:"+resultCode);
if (requestCode==0){
if (resultCode==RESULT_OK) {
setResult(RESULT_OK);
this.finish();
}
}
if(requestCode==1){
Uri imageUri = data.getData();
tv1.setText(imageUri.toString());
InputStream instream;
try {
instream = getContentResolver().openInputStream(imageUri);
Bitmap image = BitmapFactory.decodeStream(instream);
setuser_headimg.setImageBitmap(image);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}