Friday, August 6, 2010

Show Internal and External Media Files present in Phone In android

AIM: To Show Internal Ringtones present in Phone In android .
Solution:
Step 1) Get Content from AUDIO.MEDIA Class


List < MediaFile > songsList=new ArrayList < MediaFile > ();

//Code to get Internal Media files.

Cursor  internalRingtonescursor,externalRingtonesCursor;
internalRingtonescursor = this.managedQuery(Audio.Media.INTERNAL_CONTENT_URI, null, null, null, Audio.Media.DEFAULT_SORT_ORDER);
   startManagingCursor(internalRingtonescursor);
   if (internalRingtonescursor.moveToFirst()) {
while (!internalRingtonescursor.isAfterLast()) {
eachcontact = LoadRecord(internalRingtonescursor);
songsList.add(eachcontact);
internalRingtonescursor.moveToNext();
}
}


 // Load external media Files
externalRingtonesCursor =  this.managedQuery(Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, Audio.Media.DEFAULT_SORT_ORDER);
   startManagingCursor(externalRingtonesCursor);
   if (externalRingtonesCursor.moveToFirst()) {
while (!externalRingtonesCursor.isAfterLast()) {
eachcontact = LoadRecord(externalRingtonesCursor);
songsList.add(eachcontact);
externalRingtonesCursor.moveToNext();
}
}

// The method to load all the attributes of each media file.

 private MediaFile LoadRecord(Cursor cursor) {
MediaFile contactData = new MediaFile();
String[] ColumnNames = cursor.getColumnNames();
for (int intLoop = 0; intLoop < ColumnNames.length; intLoop++) {

if (Audio.Media.TITLE.compareTo(ColumnNames[intLoop]) == 0)
contactData.file_Name = cursor.getString(intLoop);
else if (Audio.Media. SIZE.compareTo(ColumnNames[intLoop]) == 0)
contactData.file_length = cursor.getLong(intLoop);
else if (Audio.Media. DATA.compareTo(ColumnNames[intLoop]) == 0)
contactData.file_Path = cursor.getString(intLoop);
}

return contactData;
}

// MediaFile class

public class MediaFile {
public String file_Name;
public String file_Path;
public Long file_length;
}


Now you will have all the values in   songsList arrayList.By accssing " songsList "  you can get all the values.


1 comment:

Android Developers Blog

Ram's shared items