我想要代码从照片库中获取照片并将该照片添加到Android中的个人资料中。
答案 0 :(得分:3)
您可以启动特定的Intent以从设备获取照片。
首先,为Intent结果代码定义一个常量,例如:
private static final int SELECT_PICTURE_ACTIVITY_RESULT_CODE = 0;
然后,在必要时,调用意图:
Intent photoPickerIntent = new Intent();
photoPickerIntent.setType("image/*"); // to pick only images
photoPickerIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(photoPickerIntent, SELECT_PICTURE_ACTIVITY_RESULT_CODE);
最后,实现Activity.onActivityResult(int, int, Intent)方法以获取所选图像的URI:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case SELECT_PICTURE_ACTIVITY_RESULT_CODE:
Uri selectedImageUri = data.getData();
// deal with it
break;
default:
// deal with it
break;
}
}
}