任何人都可以向我解释为什么这段代码允许我拍照并在图片按钮中显示,但是当我尝试从图库中获取图片时却不能。它没有显示任何错误。
final CharSequence[] items = {"Take Photo", "Choose from Library", "Cancel"};
final AlertDialog.Builder constructor = new AlertDialog.Builder(this);
constructor.setTitle("Añadir Foto!");
constructor.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
PROFILE_PIC_COUNT = 1;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
} else if (items[item].equals("Choose from Library")) {
PROFILE_PIC_COUNT = 1;
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent,SELECT_FILE);
} else if (items[item].equals("Cancel")) {
PROFILE_PIC_COUNT = 0;
dialog.dismiss();
}
}
});
img_header.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
constructor.show();
}
});
这是onActivityResult方法:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent_imagen) {
super.onActivityResult(requestCode, resultCode, intent_imagen);
switch(requestCode) {
case 0:
if(resultCode == RESULT_OK){
setFoto(intent_imagen);
}
break;
case 1:
if(resultCode == RESULT_OK){
setFoto(intent_imagen);
}
break;
}
}
private void setFoto(Intent intent_imagen) {
Bitmap foto;
foto=getCroppedBitmap(Bitmap.createScaledBitmap((Bitmap)intent_imagen.getExtras().get("data"), 190, 165, true));
img_header.setForeground(null);
img_header.setImageBitmap(foto);
}