how to get selected image from gallery in android?

后端 未结 4 905
梦谈多话
梦谈多话 2020-12-18 15:58

I have a problem: I have a function that brings me the image from the gallery ;but when I select the image, I don\'t get it.

public void funzione(View v){
in         


        
4条回答
  •  伪装坚强ぢ
    2020-12-18 16:17

    **Put This in Action Listener from where to choose image **

    Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(i, 100);
    

    This second Action Listener

    Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(i, 200);
    

    After Return from a gallery, Result get Through request code **here code is 100 and 200 for both **

    @Override
        protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            if (requestCode == 100 && resultCode == RESULT_OK && data != null){
                Uri imageUri=data.getData();
                try {
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
                    reg_certificate.setImageBitmap(bitmap);
    
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
            }
    
            if(requestCode == 200 && resultCode == RESULT_OK && data !=null){
                Uri imageUri=data.getData();
                try {
                    Bitmap bitmap= MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
                    org_logo.setImageBitmap(bitmap);
    
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
            }
        }
    

提交回复
热议问题