Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

Listing image files in a directory and populate a ListView from it in Android

Posted on August 6, 2018August 4, 2020 by Habibie

I have a directory in sdcard called “mydir” and there are lot of .png image files with another files. Then I want to list only that .png files and populate a ListView from it.

I do it like this:

String path = "/sdcard/mydir";
File directory = new File(path);
if (!directory.exists()){
	directory.mkdir();
}
File[] files = directory.listFiles();

String[] localfiles = new String[files.length];
for (int i = 0; i < files.length; i++)
{
	localfiles[i] = files[i].getName();
}
ArrayAdapter<String> lbadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, localfiles);


List<String> localfiles = new ArrayList<String>();
for (int i = 0; i < files.length; i++)
{
	if(files[i].getName().contains(".png"))
		localfiles.add(files[i].getName());
}
ArrayAdapter<String> lbadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, localfiles);

lblist.setAdapter(lbadapter);

lblist.setOnItemClickListener(new AdapterView.OnItemClickListener() {

	@Override
	public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
		final String itemname = (String) parent.getItemAtPosition(position);
		Toast.makeText(getApplicationContext(), itemname, Toast.LENGTH_LONG).show();
	}

});

 

Post Views: 387
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Hover Reveal script for Unity to show and hide object on mouse hover
  • How to Prevent UI Clicks in Unity from “Bleeding Through” to 3D Objects Behind Them
  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
© 2026 ThirteeNov | Powered by Superbs Personal Blog theme