Android: How to load an image from a file and set on an ImageView

If you’re working with an Android application, this source code works as a way to load an image from a file:

Bitmap bitmap = BitmapFactory.decodeFile(pathToPicture);

The Bitmap and BitmapFactory classes are located in the android.graphics package:

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

Assuming that your pathToPicture is correct, you can then add this bitmap image to an ImageView like this:

ImageView imageView = (ImageView) getActivity().findViewById(R.id.imageView);
imageView.setImageBitmap(BitmapFactory.decodeFile(pathToPicture));

As shown, the setImageBitmap method is another key to this solution.

I’m currently using this approach to let users choose an image from their image/photo gallery, which is where I get the file path.