0

I start the chooser activity

selectButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Open from"), PICK_IMAGE);
    }
});

and I have to change an imageview picture with the result, I'm not sure but the resultCode variable from onActivityResult has -1 value

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PICK_IMAGE) {
        //get data and change imageView
    }
}

But I don't know get the data from the Intent data to set the picture of the imageView

Adeel
  • 2,901
  • 7
  • 24
  • 34
AFS
  • 1,433
  • 6
  • 28
  • 52

2 Answers2

1

-1 is the constant value for RESULT_OK which means your activity that was started for a result completed successfully.

Use your debugger to break on onActivityResult method. Inspect the data Intent you get back. It should have either:

1: a Bitmap object that is the image that was obtained or

2: a URI that is the path to the image you can then load using something like Glide or Picasso.

See this section in the docs for more.

dominicoder
  • 9,338
  • 1
  • 26
  • 32
1

the -1 in the resultCode means that the action was successful. See here

The path to the File you find in the Intent data. You can access it like this:

Uri uri = data.getData();

Then you can set the ImageView to this URI:

ImageView.setImageURI(uri);

But thanks to the comment and according to the docs this is not recommended. As alternative use ImageView.setImageDrawable(Drawable) or ImageView.setImageBitmap(android.graphics.Bitmap) as stated in the docs.

So here they explain how to use setImageBitmap: https://stackoverflow.com/a/4717740/6845698

And here how to get a drawable from an Uri, which you can set then with setImageDrawable: https://stackoverflow.com/a/16718935/6845698

Which to use depends on your needs like file type.

wirthra
  • 153
  • 1
  • 10
  • `setImageURI()` blocks the main application thread for loading and decoding the image. This approach is not recommended. – CommonsWare Nov 19 '17 at 19:22