Reputation: 11
I have an (admittedly very large) for loop that keeps ending early. It first locates all songs in a user's music folder through a recursive method, and then adds them into a database for faster retrieval in the future. I have it set up on an ASync, the relevant portions of which are below.
private void updateSongsDB() {
ArrayList<File> fileList = getAllDirectories(mCurrentRootFile);
int numDirectories = fileList.size();
int numScanned = 0;
resetDB();
// Setup the strings
String title;
String artist;
String album;
String duration;
String genre;
String track_number;
String total_tracks;
try{
for(File file : fileList) {
try {
MediaMetadataRetriever mMetaDataRetriever = new MediaMetadataRetriever();
mMetaDataRetriever.setDataSource(file.getAbsolutePath());
if (mMetaDataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_AUDIO) != null) {
// Extract the information
title = mMetaDataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
title = (title != null) ? title : "";
artist = mMetaDataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
artist = (artist != null) ? artist : "";
album = mMetaDataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
album = (album != null) ? album : "";
duration = mMetaDataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
duration = (duration != null) ? duration : "";
genre = mMetaDataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE);
genre = (genre != null) ? genre : "";
track_number = mMetaDataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_CD_TRACK_NUMBER);
track_number = (track_number != null) ? track_number : "";
total_tracks = mMetaDataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_NUM_TRACKS);
total_tracks = (total_tracks != null) ? total_tracks : "";
insertDBRow(title, artist, album, duration, genre, track_number, total_tracks, file.getAbsolutePath());
}
}
catch (IllegalArgumentException e) {
// This is not a playable media file, ignore it
}
numScanned++;
publishProgress((float) ((numScanned / (float) numDirectories) * 100));
// Escape early if cancel() is called
//if (isCancelled()) break;
}
}
catch(Exception e) {
// No files in the list
}
Log.d("Total Files", String.valueOf(numDirectories));
Log.d("Number Scanned", String.valueOf(numScanned));
}
private void resetDB() {
mSongListDB.execSQL(SongReaderContract.SQL_DELETE_ENTRIES);
mSongListDB.execSQL(SongReaderContract.SQL_CREATE_ENTRIES);
}
private ArrayList<File> getAllDirectories(File rootFile) {
File[] fileList = rootFile.listFiles();
ArrayList<File> dirs = new ArrayList<>();
try {
for (File file : fileList) {
if (file.isDirectory()) {
dirs.addAll(getAllDirectories(file));
} else {
dirs.add(file);
}
}
}
catch(Exception e) {
// No files in the list
}
return dirs;
}
private void insertDBRow(String title, String artist, String album, String duration,
String genre, String track_number, String total_tracks, String path) {
ContentValues values = new ContentValues();
values.put(SongReaderContract.FeedEntry.COLUMN_NAME_TITLE, title);
values.put(SongReaderContract.FeedEntry.COLUMN_NAME_ARTIST, artist);
values.put(SongReaderContract.FeedEntry.COLUMN_NAME_ALBUM, album);
values.put(SongReaderContract.FeedEntry.COLUMN_NAME_DURATION, duration);
values.put(SongReaderContract.FeedEntry.COLUMN_NAME_GENRE, genre);
values.put(SongReaderContract.FeedEntry.COLUMN_NAME_TRACK_NUMBER, track_number);
values.put(SongReaderContract.FeedEntry.COLUMN_NAME_TOTAL_TRACKS, total_tracks);
values.put(SongReaderContract.FeedEntry.COLUMN_NAME_PATH, path);
// Insert the new row
mSongListDB.insert(SongReaderContract.FeedEntry.TABLE_NAME, null, values);
}
On my testing device, I have 1774 songs. The fileList is being populated just fine, as my LogCat displays 1774 total files. However, no matter what I do, the for loop always stops at either 978, or 979 iterations, as evidenced in my logs below.
03-17 22:36:40.011 10927-11112/com.demo41357.simplemusicplayer D/mali_winsys: new_window_surface returns 0x3000, [1440x2560]-format:1
03-17 22:36:40.161 10927-10927/com.demo41357.simplemusicplayer W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
03-17 22:36:40.161 10927-10927/com.demo41357.simplemusicplayer I/InjectionManager: dispatchCreateOptionsMenu :com.demo41357.simplemusicplayer.MainActivity
03-17 22:36:40.161 10927-10927/com.demo41357.simplemusicplayer I/InjectionManager: dispatchPrepareOptionsMenu :com.demo41357.simplemusicplayer.MainActivity
03-17 22:36:40.231 10927-10927/com.demo41357.simplemusicplayer D/ViewRootImpl: #1 mView = android.widget.LinearLayout{b83ac55 V.E...... ......I. 0,0-0,0}
03-17 22:36:40.241 10927-10927/com.demo41357.simplemusicplayer D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=1
03-17 22:36:40.301 10927-11112/com.demo41357.simplemusicplayer D/mali_winsys: new_window_surface returns 0x3000, [296x176]-format:1
03-17 22:36:40.311 10927-10927/com.demo41357.simplemusicplayer D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
03-17 22:36:40.311 10927-10927/com.demo41357.simplemusicplayer I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@3878aec time:88802249
03-17 22:36:41.831 10927-10927/com.demo41357.simplemusicplayer D/ViewRootImpl: #3 mView = null
03-17 22:36:41.841 10927-10927/com.demo41357.simplemusicplayer D/ViewRootImpl: #1 mView = android.widget.LinearLayout{cce9e37 V.E...... ......I. 0,0-0,0}
03-17 22:36:41.891 10927-11112/com.demo41357.simplemusicplayer D/mali_winsys: new_window_surface returns 0x3000, [296x176]-format:1
03-17 22:36:41.901 10927-10927/com.demo41357.simplemusicplayer D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
03-17 22:36:43.841 10927-10927/com.demo41357.simplemusicplayer D/ViewRootImpl: #3 mView = null
03-17 22:36:45.321 10927-10927/com.demo41357.simplemusicplayer D/ViewRootImpl: ViewPostImeInputStage processPointer 0
03-17 22:36:45.421 10927-10927/com.demo41357.simplemusicplayer D/ViewRootImpl: ViewPostImeInputStage processPointer 1
03-17 22:36:45.441 10927-10927/com.demo41357.simplemusicplayer I/InjectionManager: dispatchPrepareOptionsMenu :com.demo41357.simplemusicplayer.MainActivity
03-17 22:36:45.471 10927-10927/com.demo41357.simplemusicplayer I/ListPopupWindow: Could not find method setEpicenterBounds(Rect) on PopupWindow. Oh well.
03-17 22:36:45.521 10927-10927/com.demo41357.simplemusicplayer D/AbsListView: Get MotionRecognitionManager
03-17 22:36:45.521 10927-10927/com.demo41357.simplemusicplayer E/MotionRecognitionManager: mSContextService = android.hardware.scontext.ISContextService$Stub$Proxy@bcd8327
03-17 22:36:45.521 10927-10927/com.demo41357.simplemusicplayer E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@ba0e9d4
03-17 22:36:45.521 10927-10927/com.demo41357.simplemusicplayer E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@ba0e9d4
03-17 22:36:45.531 10927-10927/com.demo41357.simplemusicplayer D/ViewRootImpl: #1 mView = android.widget.PopupWindow$PopupDecorView{5ad8672 V.E...... ......I. 0,0-0,0}
03-17 22:36:45.561 10927-11112/com.demo41357.simplemusicplayer D/mali_winsys: new_window_surface returns 0x3000, [1040x640]-format:1
03-17 22:36:45.651 10927-10927/com.demo41357.simplemusicplayer D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
03-17 22:36:46.641 10927-10927/com.demo41357.simplemusicplayer D/ViewRootImpl: ViewPostImeInputStage processPointer 0
03-17 22:36:46.711 10927-10927/com.demo41357.simplemusicplayer D/ViewRootImpl: ViewPostImeInputStage processPointer 1
03-17 22:36:46.781 10927-10927/com.demo41357.simplemusicplayer I/InjectionManager: dispatchOptionsItemSelected :com.demo41357.simplemusicplayer.MainActivity
03-17 22:36:47.181 10927-11112/com.demo41357.simplemusicplayer D/OpenGLRenderer: endAllActiveAnimators on 0x7f6e5ee800 (MenuPopupWindow$MenuDropDownListView) with handle 0x7f85e78c20
03-17 22:36:47.181 10927-10927/com.demo41357.simplemusicplayer D/ViewRootImpl: #3 mView = null
03-17 22:36:59.311 10927-11976/com.demo41357.simplemusicplayer D/Total Files: 1774
03-17 22:36:59.311 10927-11976/com.demo41357.simplemusicplayer D/Number Scanned: 978
03-17 22:36:59.311 10927-10927/com.demo41357.simplemusicplayer D/POPUP_DISMISSAL: The post execute script ran
The POPUP_DISMISSAL log is called from my ASync's onPostExecute function. I checked if it was a memory issue, but while running the memory never went over 13MB of 20MB available, and CPU usage never got over 10%. I am at a complete loss. I've tried reformatting the for loop to read:
for (int curFile=0; curFile<numDirectories; curFile++)
but that code also returns the exact same 978 or 979 entries. I also tried passing the entire for loop into a separate ArrayList, instead of inserting directly, and then looping through that list to insert, but the first for loop still only put 978 or 979 items into that ArrayList. I suppose I could split it into separate for loops that only handle say, 500 iterations each, but that seems messy and poor practice, as well as I don't even know if it would work.
Any help would be greatly appreciated.
Upvotes: 0
Views: 91
Reputation: 11
Turns out I was catching the wrong Exceptions in my MediaMetaData object so when it reached a file that did not initialize properly it threw a RunTime Exception into my loop, ending the loop itself.
Upvotes: 1