Easiest way to get the title of a file ?

All your problems with Audio, Video and Images.

Easiest way to get the title of a file ?

Postby Woulou » Fri Jul 09, 2010 2:01 pm

Hello,

In my app I'm trying to make a listview which makes a list of all the files of a folder (the files will be images or HTML pages). Of course I can make a String with the name of each items by hand like
items String[] = {"file1","file2", etc ... } and then put it in the listview.

but first I've got many files and second I'd like to be able to place a file in the folder and the list will automaticly update (basically I don't want to write some code each time I put something in the folder.)
So I'd prefer something like
items String[] = get.title of my files :P

Could you help me or give a link to a tutorial about it (if there's any?)

Thanks
Woulou
Junior Developer
Junior Developer
 
Posts: 16
Joined: Wed Jul 07, 2010 3:49 pm

Top

Re: Easiest way to get the title of a file ?

Postby Woulou » Sat Jul 10, 2010 12:59 pm

Any help ? I think we can do it with listFile() but I can't achieve it...
Woulou
Junior Developer
Junior Developer
 
Posts: 16
Joined: Wed Jul 07, 2010 3:49 pm

Re: Easiest way to get the title of a file ?

Postby Woulou » Sat Jul 10, 2010 2:15 pm

Eventually I found a solution which is

Code: Select all
private List<String> listItems = new ArrayList<String>() ;

File f = new File("/sdcard");
          if (f.listFiles().length > 0) {
             for (File file : f.listFiles()) {
                listItems.add(file.getName());}
                }             


and then I put "ListItems" in my ListView.

My problem is that my folder isn't in "/sdcard" but in my internal folders of my application, it is in /res/raw. And if I put "res/raw" or "res.raw" in the code it doesn't work (the app crashes :().
So how can I get the path to res/raw ? Pleeeeaaaaase :)
Woulou
Junior Developer
Junior Developer
 
Posts: 16
Joined: Wed Jul 07, 2010 3:49 pm

Re: Easiest way to get the title of a file ?

Postby Woulou » Sat Jul 10, 2010 4:14 pm

Ok, still no answer... but for those who may be interested, I've found it !!! I don't understand why it works but it does lol :P. In fact I'm not using listFile() but getFields()... Here's my code

Code: Select all
private List<String> listItems = new ArrayList<String>() ;

Field[] fields = R.raw.class.getFields();
          for(Field f : fields)
             try {
                   listItems.add(f.getName());
                 } catch (IllegalArgumentException e) {
                 }

I found my solution thanks to this post http://stackoverflow.com/questions/2803566/accesing-contents-of-res-raw-programatically-android
Woulou
Junior Developer
Junior Developer
 
Posts: 16
Joined: Wed Jul 07, 2010 3:49 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 6 guests