gpt4 book ai didi

android - 如何从 fragment 上的画廊获取图像?

转载 作者:太空宇宙 更新时间:2023-11-03 12:04:27 25 4
gpt4 key购买 nike

我想制作一个 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();

关于android - 如何从 fragment 上的画廊获取图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39547728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com