我想創建一個具有功能的片段,就好像我按下了一個按鈕,然後畫廊將被打開,然後我選擇一個圖像,並將該圖像放入我的圖像視圖中。如何從圖片庫中獲取圖片?
我寫的代碼,
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)
,什麼都沒有發生。
我的錯在哪裏?請告訴我。
你得到任何錯誤?如果是這樣發佈錯誤日誌。 – RamithDR