How to pick an image from gallery (SD Card) for my app?

前端 未结 10 1879
Happy的楠姐
Happy的楠姐 2020-11-21 23:40

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

10条回答
  •  一向
    一向 (楼主)
    2020-11-22 00:00

    For some reasons, all of the answers in this thread, in onActivityResult() try to post-process the received Uri, like getting the real path of the image and then use BitmapFactory.decodeFile(path) to get the Bitmap.

    This step is unnecessary. The ImageView class has a method called setImageURI(uri). Pass your uri to it and you should be done.

    Uri imageUri = data.getData();
    imageView.setImageURI(imageUri);
    

    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 prevent OurOfMemoryError, like shown in the @siamii answer.

提交回复
热议问题