我想制作一个 fragment ,它的功能就好像我按下一个按钮,然后画廊就会打开,我选择一张图片,然后该图片就会被放入我的 ImageView 中。
我写代码,
public class GreenFragment extends Fragment {
ImageView mImageview;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_green, container, false);
mImageview = (ImageView) view.findViewById(R.id.iv_beforesendpost);
view.findViewById(R.id.btn_getgallery).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int permissionCheck = ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.READ_EXTERNAL_STORAGE);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
startGallery();
} else {
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
2000);
}
}
});
return view;
}
private void startGallery() {
Intent cameraIntent = new Intent(Intent.ACTION_GET_CONTENT);
cameraIntent.setType("image/*");
if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(cameraIntent, 1000);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1000 && resultCode == Activity.RESULT_OK) {
}
Uri returnUri;
returnUri = data.getData();
Glide.with(this)
.load(returnUri)
.override(1280, 1280)
.centerCrop()
.crossFade()
.into(mImageview);
但是当我点击 Button (btn_gatgallery)
时,没有任何反应。
我错在哪里?请告诉我。
view.findViewById(R.id.btn_getgallery).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(ActivityCompat.checkSelfPermission(getActivity(),
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
{
requestPermissions(
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
2000);
}
else {
startGallery();
}
}
});
return view;
}
添加
Intent cameraIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
处理请求
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//super method removed
if(resultCode == RESULT_OK) {
if(requestCode == 1000){
Uri returnUri = data.getData();
Bitmap bitmapImage = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), returnUri);
your_imageView.setImageBitmap(bitmapImage);
}
}
//Uri returnUri;
//returnUri = data.getData();
我是一名优秀的程序员,十分优秀!