I am getting file path of image by using browse button....after that I want to set this image to an image view using the file path
Asked
Active
Viewed 5.7k times
4 Answers
41
If with File
you mean a File
object, I would try:
File file = ....
Uri uri = Uri.fromFile(file);
imageView.setImageURI(uri);

rciovati
- 27,603
- 6
- 82
- 101
-
Uri imageUri = Uri.parse(ImagePath); imageView.setImageURI(imageUri); ... This works for all devices for me except Xiaomi Mi4, any idea why is that. – Avijeet Jan 09 '16 at 11:36
-
That doesn't work with large images in my device (e.g.: 1.5MB or 2MB). Any way to compress it (I simply want to display thumbnails). – Oct 09 '16 at 01:39
-
Its doesn't set correct image size, Lakshay Sharma's answer is perfect with regarding the image dimension – Naveen Kumar M May 18 '17 at 09:46
9
You can give a try to this code:
imageView.setImageBitmap(BitmapFactory.decodeFile(yourFilePath));
BitmapFactory will decode the given image file into a Bitmap object, which you will then set into the imageView object.

Ufuk Can Bicici
- 3,589
- 4
- 28
- 57
-
2This is the easiest method.Obviously, if you are only returning the file and not the path. use `file.getPath()` instead of `yourFilePath` – Apr 04 '13 at 15:05
8
To set an image from a file you need to do this:
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg"); //your image file path
mImage = (ImageView) findViewById(R.id.imageView1);
mImage.setImageBitmap(decodeSampledBitmapFromFile(file.getAbsolutePath(), 500, 250));
When decodeSampledBitmapFromFile
:
public static Bitmap decodeSampledBitmapFromFile(String path,
int reqWidth, int reqHeight) { // BEST QUALITY MATCH
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
options.inPreferredConfig = Bitmap.Config.RGB_565;
int inSampleSize = 1;
if (height > reqHeight) {
inSampleSize = Math.round((float)height / (float)reqHeight);
}
int expectedWidth = width / inSampleSize;
if (expectedWidth > reqWidth) {
//if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
inSampleSize = Math.round((float)width / (float)reqWidth);
}
options.inSampleSize = inSampleSize;
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
You can play with the numbers (500 and 250 in this case) to change the quality of the bitmap for the ImageView
.

Emil Adz
- 40,709
- 36
- 140
- 187
2
To load an image from a file:
Bitmap bitmap = BitmapFactory.decodeFile(pathToPicture);
Assuming that your pathToPicture
is correct, you can then add this bitmap image to an ImageView
like
ImageView imageView = (ImageView) getActivity().findViewById(R.id.imageView);
imageView.setImageBitmap(BitmapFactory.decodeFile(pathToPicture));

HebeleHododo
- 3,620
- 1
- 29
- 38

Lakshay Sharma
- 1,347
- 12
- 13