在我的应用中,我想将图像放在选择用户图像的位置。在我的片段中,我有一个图像视图和一个按钮,当我从图库中选择图像时,我无法将图像放在图像视图上。
这是我的代码:
个人资料片段:
public class ProfileFragment extends Fragment {
//private static Bitmap Image = null;
private ImageView imageView;
private static final int SELECT_PHOTO = 1;
Button browseProfilePic;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout_profilepic, container, false);
imageView = (ImageView)view.findViewById(R.id.profile_image);
browseProfilePic = (Button) view.findViewById(R.id.btn_pick);
browseProfilePic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case SELECT_PHOTO:
if(resultCode == getActivity().RESULT_OK){
try {
final Uri imageUri = imageReturnedIntent.getData();
final InputStream imageStream = getActivity().getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
imageView.setImageBitmap(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
}
我的活动:
public class AccountActivity extends FragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.account);
String item = getIntent().getStringExtra("item");
Fragment fragment = null;
switch(item) {
case "Balance": fragment = new BalanceFragment(); break;
case "Personal": fragment = new PersonalFragment(); break;
case "Work": fragment = new WorkFragment(); break;
case "Address": fragment = new AddAddressFragment(); break;
case "Vehicle": fragment = new AddVehicleFragment(); break;
case "Preference": fragment = new PreferenceFragment(); break;
case "About": fragment = new AboutFragment(); break;
case "Settings": fragment = new SettingsFragment(); break;
case "Help": fragment = new HelpFragment(); break;
case "Profile":fragment = new ProfileFragment(); break;
case "Logout":logout();break;
default: fragment = new DefaultFragment(); break;
}
if (savedInstanceState == null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// AccountFragment fragment = new AccountFragment();
transaction.replace(R.id.account_frame, fragment);
transaction.commit();
}
setTitle(item);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode,resultCode,data);
}
请有人帮助我。
答案 0 :(得分:0)
在托管片段的活动中,请务必添加以下行:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
查看此帖子了解更多信息Android onActivityResult not called from fragment