Skip to content

Instantly share code, notes, and snippets.

@mubin986
Created December 8, 2018 13:50
Show Gist options
  • Save mubin986/ce2cafdee1da92b49e2bc963ccca22fc to your computer and use it in GitHub Desktop.
Save mubin986/ce2cafdee1da92b49e2bc963ccca22fc to your computer and use it in GitHub Desktop.
private static final int REQUEST_CODE = 1;
private Bitmap bitmap;
// on click listener
reg_photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_CODE);
}
});
// result code
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
try {
// We need to recyle unused bitmaps
if (bitmap != null) {
bitmap.recycle();
}
InputStream stream = getActivity().getContentResolver().openInputStream(
data.getData());
bitmap = BitmapFactory.decodeStream(stream);
stream.close();
reg_photo.setImageBitmap(bitmap); // youe own view
bitmap = ((BitmapDrawable) reg_photo.getDrawable()).getBitmap();
picIsSelected = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
super.onActivityResult(requestCode, resultCode, data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment