通过查看和查看许多教程,我一直在寻找2天的答案,但我没有找到任何可以帮助我的方法。
我遇到了ImageView问题。我想通过从手机图库中选择一张图片来更改图像。我正在使用Android Studio。
如果你们能帮助我解决这个问题,我将非常感激。如果您需要任何进一步的信息,请不要犹豫与我联系。
提前致谢。来自瑞士的问候。
public class AddCategory extends Fragment{
private static final int IMAGE_PICKER_SELECT = 1;
Button b;
ImageView imageView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_addcategory, container, false);
imageView = (ImageView)rootView.findViewById(R.id.imageView_add);
b = (Button)rootView.findViewById(R.id.button_add);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i,IMAGE_PICKER_SELECT );
}
});
return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == IMAGE_PICKER_SELECT && requestCode == Activity.RESULT_OK && null != data){
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView)getActivity().findViewById(R.id.imageView_add);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
} else {
Toast.makeText(getActivity(), "Try Again!!", Toast.LENGTH_SHORT).show();
}
}
}