This question was originally asked for Android 1.6.
I am working on photos options in my app.
I have a button and an ImageView in my Activity. When I click the button it would redirect to the gallery and I would be able to select an image. The selected image would appear in my ImageView.
Here is a tested code for image and video.It will work for all APIs less than 19 and greater than 19 as well.
Image:
Video:
.
For some reasons, all of the answers in this thread, in
onActivityResult()
try to post-process the receivedUri
, like getting the real path of the image and then useBitmapFactory.decodeFile(path)
to get theBitmap
.This step is unnecessary. The
ImageView
class has a method calledsetImageURI(uri)
. Pass your uri to it and you should be done.For a complete working example you could take a look here: http://androidbitmaps.blogspot.com/2015/04/loading-images-in-android-part-iii-pick.html
PS:
Getting the
Bitmap
in a separate variable would make sense in cases where the image to be loaded is too large to fit in memory, and a scale down operation is necessary to preventOurOfMemoryError
, like shown in the @siamii answer.