Select image from gallery and show in imageview

后端 未结 3 736
一个人的身影
一个人的身影 2020-12-12 06:48

I need Open my gallery in mobile phone and select one picture which opened in imageview on activity.. nothing hard.. but I have code and thise code in simulator (genymotion)

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-12 07:04

    put this code inside (anything which you use to go to gallery)

    Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
    

    Then call this function

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
            Uri filePath = data.getData();
    
               Bitmap img=MediaStore.Images.Media.getBitmap(getContentResolver, filePath);
    
    
         If(img!= null) {
                image.setImageBitmap(img);
        }
    }
    

    don't forget this permission to be called

    
    
    

提交回复
热议问题