1

I am not able to find out how to set a picture as setImageResource(). Currently I can easily add from res/drawable like this:

In ListViewAdapter:

imageView = (ImageView) rowView.findViewById(R.id.listImageView); 
    imageView.setImageResource((int) getItemId(position)); 

In ViewAdapter:

public class ViewAdapter extends BaseAdapter { 
    class Item { 
        public long id;
        public String name; 

    public Item(long id, String name) { 
            this.id = id;
            this.name = name; 
    } 
} 
public ViewAdapter(Context c) { 
    mContext = c; 

    mItems = new ArrayList<Item>();

    mItems.add(new Item(R.drawable.mercury,"Mercury")); 
    mItems.add(new Item(R.drawable.mercury,"Mercury")); 
    mItems.add(new Item(R.drawable.mercury,"Mercury")); ...

So, how can I add a picture from a path? For example, say "/storage/emulated/Photos/Italy.jpg"? Again, I can store them in res/drawable and add them, but need a way to add it from file. Tried different ways, but still can't find out how to do it.

Thanks a lot!

Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85
Nurgul Alshyn
  • 79
  • 3
  • 11

4 Answers4

2

Have you tried

setImageURI(Uri uri)

see this :

How to use setImageUri() on Android

Community
  • 1
  • 1
Saif
  • 6,804
  • 8
  • 40
  • 61
2
File imgFile = new  File(“/sdcard/Images/test_image.jpg”);

if(imgFile.exists()){

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
    myImage.setImageBitmap(myBitmap);

}
Zoker
  • 2,020
  • 5
  • 32
  • 53
Yogesh Ude
  • 121
  • 5
0

Try this way,hope this will help you to solve your problem.

Bitmap bitmap = BitmapFactory.decodeFile(filepath);
imagView.setImageBitmap(bitmap);
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0

You can set picture from path

    File img = new  File("here your path"");
    if(img.exists()){

        Bitmap myBitmap = BitmapFactory.decodeFile(img.getAbsolutePath());

        ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
        myImage.setImageBitmap(myBitmap);

  }
Divyang Metaliya
  • 1,908
  • 1
  • 12
  • 14