Tin_Ram
Tin_Ram

Reputation: 99

Displaying from external storage?

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!

Upvotes: 0

Views: 63

Answers (4)

Tin_Ram
Tin_Ram

Reputation: 99

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);

Upvotes: 0

Shabbir Dhangot
Shabbir Dhangot

Reputation: 9121

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()));

Upvotes: 1

Keyur Lakhani
Keyur Lakhani

Reputation: 4371

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()));

Upvotes: 1

Ankit Aggarwal
Ankit Aggarwal

Reputation: 5375

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" />

Upvotes: 1

Related Questions