K. Barresi
K. Barresi

Reputation: 1315

Android: Populate Gallery With Images from SDCard

I'm trying to populate a Gallery view with images from a folder in the SDcard. I read the Gallery tutorial on Android Dev seen here: http://developer.android.com/resources/tutorials/views/hello-gallery.html, and can get it working with images from the drawable folder of my project.

My question is: how can I get an array of files from the Environment.getExternalStorageDirectory()+File.separator+"MyPictureDirectory folder, and then display them in the Gallery? Thanks!

Upvotes: 0

Views: 1687

Answers (1)

Ponmalar
Ponmalar

Reputation: 7031

Try the following,

File file = new File( Environment.getExternalStorageDirectory()+File.separator+"MyPictureDirectory"+File.separator);
File imageList[] = file.listFiles();
for(int i=0;i<imageList.length;i++)
{
   //Add images in Gallery from imageList
}

To set image from file path:

File imgFile = new  File(imagefilepath);
if(imgFile.exists()){

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

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

}

Upvotes: 1

Related Questions