Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get image from gallery on fragment?

I want to make a fragment that has function as if i push a button, and then gallery would be opened, and i choose a image, and that image would be put into my imageview.

I wrote code,

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

But when i clicked Button (btn_gatgallery), nothing happened.

Where is my fault? Please let me know.

like image 648
H.fate Avatar asked Sep 17 '16 14:09

H.fate


People also ask

How to pick an image from Image Gallery in Android?

How to pick an image from image gallery in Android? This example demonstrates how do I pick an image from image gallery in android app. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

How to implement the layout of the application using image selector?

The main layout of the application includes one button to open the image selector, and one Image View to preview the selected image from the gallery. To implement the layout of the application, invoke the following code inside the activity_main.xml file.

How do I take a picture with a camera intent?

A camera intent example. When you select "Take Photo," the external camera app will pop open and you are able to take an image. The results will be displayed in the main window and a "thumbnail" result will appear in the smaller box.

How to select a photo from the gallery in Kotlin?

We need a function to select a photo from the gallery. We are coding the function named chooseImageGallery. In it we define the variable intent and specify its type. Note: We cannot create Static Properties and Static Function in Kotlin. Therefore, we need to use Companion Object to become a static properties.


1 Answers

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;
    }

Add

Intent cameraIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

Handle request

@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();
like image 166
W4R10CK Avatar answered Nov 14 '22 23:11

W4R10CK