如何从片段上的图库中获取图像?

时间:2016-09-17 14:06:12

标签: android android-fragments

我想创建一个具有功能的片段,就好像我按下一个按钮,然后画廊将被打开,我选择一个图像,该图像将被放入我的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)时,什么也没发生。

我的错在哪里?请告诉我。

1 个答案:

答案 0 :(得分:6)

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();