Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Pick an image on Android
private static final int PICK_PHOTO_FOR_AVATAR = 0;
public void pickImage() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICK_PHOTO_FOR_AVATAR);
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_PHOTO_FOR_AVATAR && resultCode == Activity.RESULT_OK) {
if (data == null) {
//Display an error
InputStream inputStream = context.getContentResolver().openInputStream(data.getData());
//Now you can do whatever you want with your inpustream, save it as file, upload to a server, decode a bitmap...

This comment has been minimized.

Copy link

@juanjosv juanjosv commented Feb 22, 2017

try {
InputStream inputStream = getContext().getContentResolver().openInputStream(data.getData());
} catch (FileNotFoundException e) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment