**private static final int REQUEST_CODE = 1;
private Button button_1;
public int TAKE_PICTURE = 1;
private ImageView image_view;
private Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sell);
image_view = (ImageView) findViewById(R.id.resul);
button_1 = (Button) findViewById(R.id.button4);
button_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, TAKE_PICTURE);
}
});
Button buttonLoadImage = (Button) findViewById(R.id.button3);
buttonLoadImage.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
try {
// We need to recyle unused bitmaps
if (bitmap != null) {
bitmap.recycle();
}
InputStream stream = getContentResolver().openInputStream(data.getData());
bitmap = BitmapFactory.decodeStream(stream);
stream.close();
image_view.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onActivityResult2(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Uri targetUri = data.getData();
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
image_view.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
在这里输入代码
答案 0 :(得分:1)
您不需要为两个onActivityResult()
调用创建两个startActivityForResult
方法。只需在startActivityForResult
调用中提供不同的请求代码即可。并且onActivityResult
会通过给定的请求代码通知您,以确定结果。
private static final int TAKE_PICTURE = 100;
private static final int CHOOSE_PICTURE = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sell);
image_view = (ImageView) findViewById(R.id.resul);
button_1 = (Button) findViewById(R.id.button4);
button_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
// Here provide TAKE_PICTURE request code to identify the IMAGE_CAPTURE result in onActivityResult method.
startActivityForResult(intent, TAKE_PICTURE);
}
});
Button buttonLoadImage = (Button) findViewById(R.id.button3);
buttonLoadImage.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK,
// Here provide CHOOSE_PICTURE request code to identify the ACTION_PICK result in onActivityResult method. android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, CHOOSE_PICTURE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PICTURE && resultCode == Activity.RESULT_OK)
// Do something with take picker result
}else if (requestCode == CHOOSE_PICTURE && resultCode == Activity.RESULT_OK)
// Do something with choose image result
}
}