-1

I have a number of videos in my sd card in the following location... "sdcard/new folder/**.mp4". Now I want to populate a listview with the names of these videos. The number of videos is going to vary every time. There is no fixed number of videos. So saving the names in the "values" folder isn't gonna do me any good. My code is below...

public class Main extends ListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

and the main.xml

     <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

I don't know how to populate the listview with the names of the videos. Please help.

Mohammad Sohaib
  • 577
  • 3
  • 11
  • 28

3 Answers3

0

You should use sqlite database to store the filenames, So that you can get the count any time. Then its easier na?

Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64
0

You should store video into SD card but can save file path into sqlite database. Then read file path and video from SD card and show in a list view .

//get the path of the sdcard and enter all the files to an array file
 File[] file = Environment.getExternalStorageDirectory().listFiles(); 

or

File dir = new File(Environment.getExternalStorageDirectory() + "/YourFolder");
File[] fileslist = dir.listFiles(); 

  for(File file:fileslist ){        
     videoList.add(file.getPath());
  }
Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37
0

You should adapt this part of code to your app

    public class MainActivity extends Activity {

        private List<String> myList;
        File file;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


            //List code
            ListView listView = (ListView) findViewById(R.id.mylist);
            myList = new ArrayList<String>();

            File directory = Environment.getExternalStorageDirectory();
            file = new File( directory + "/videos" );
            File list[] = file.listFiles(new FilenameFilter() {

                public boolean accept(File dir, String filename) {
                    // TODO Auto-generated method stub
                    if(filename.contains(".mp4")){
                        return true;
                    }
                    return false;
                }
            });

            for( int i=0; i< list.length; i++)
            {
                    myList.add( list[i].getName() );
            }
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, android.R.id.text1, myList);
            listView.setAdapter(adapter); //Set all the file in the list. 
          }
}