0

I use Picasso to get an image from the gallery and set it to an ImageView, but it does not do it. Couldn't find a problem. What is the reason? And the interesting thing is that there was no error. I tested the program through my own device.

public class MainActivity extends AppCompatActivity {
    String imageUri ;
    ImageView img ;
    private static final int GALLERY_REQUEST = 9391;
    Button b ;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK && data != null) {
            imageUri = data.getData().toString() ;
            loadImage() ;
        }
        else
        {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    private void loadImage() {
        Picasso.with(this).load(imageUri).fit().centerInside().into(img);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        img = (ImageView)findViewById(R.id.image);
        b = (Button)findViewById(R.id.button) ; // it is button used to open //a gallery
    }

    //thins function called when button pressed
    public void openGallery(View view) {
        Intent i = new Intent(ACTION_PICK,EXTERNAL_CONTENT_URI) ;

        startActivityForResult(i,GALLERY_REQUEST);
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
user202912
  • 17
  • 1
  • 6

2 Answers2

0

You called super.onActivityResult(requestCode, resultCode, data); which is wrong .

Do this

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK && data != null) {
        Uri selectedImageURI = data.getData();                 

 Picasso.with(this).load(selectedImageURI).fit().centerInside().into(img);
    }
    else
    {
        // handle this case
    }
}
Quick learner
  • 10,632
  • 4
  • 45
  • 55
0

Problem Solved.

I forgot to add permission used to read external storage.

user202912
  • 17
  • 1
  • 6