0

I have an image saved in my Pictures folder, how to display it in a imageview?

like:

imageview.setimage("//Pictures//cat.jpg)

I know it's not a correct code, but I want to achieve something like this, hope someone can help, thanks!

Tin_Ram
  • 99
  • 1
  • 11

4 Answers4

1

you first generate a bitmap from file path and then put that bitmap to imageview

File image = new File(filePath);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
imageView.setImageBitmap(bitmap);

Edit: Also add this permission in your manifest

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Ankit Aggarwal
  • 5,317
  • 2
  • 30
  • 53
1

You can set image like this from the sd card get path and create file variable and decode file using BitmapFactory set imageview image

String path = Environment.getExternalStorageState()+"/Pictures//cat.jpg";
File f = new File(path);
imageview.setImageBitmap(new BitmapFactory.decodeFile(f.getAbsolutePath()));
Keyur Lakhani
  • 4,321
  • 1
  • 24
  • 35
1

First you passing wrong file path.

for Correct File path do like this

Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES).getAbsolutePath());

then create your URL like.

String filePath = Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES).getAbsolutePath()) + "/cat.jpg";

Then use like this.

File image = new File(filePath);
imageView.setImageBitmap(new BitmapFactory.decodeFile(image.getAbsolutePath()));
Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80
0

Got it working with combination of the 2 posted codes, thanks for everyone, here's the working code:

Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES).getAbsolutePath();
    String filePath = Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES) + "/picFolder/1.jpg";
    File image = new File(filePath);
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
    image1.setImageBitmap(bitmap);
Tin_Ram
  • 99
  • 1
  • 11