0

I am trying to traverse all the folders in the file system. and I want to index all the traversed files. When I am trying to do the indexing with SQLite db, the app doesn't respond. What is the way to get it working?

  private class StartIndexing extends AsyncTask<File, String, String>{


            @Override
            protected String doInBackground(File... params) {
                String indexDone="0";
                File file=params[0];
                indexIn(file);

                indexDone="Async Done";
                return indexDone;
            }

            protected void indexIn(File folder){
                File[] dirs=folder.listFiles();

                for(File val:dirs){

                    if(val.isDirectory()){
                        new StartIndexing().executeOnExecutor(THREAD_POOL_EXECUTOR,val);

                        count++;
                    }
                    else{
                        DBoperation(val);
                        filecount++;
                    }

                }


            }
vokilam
  • 10,153
  • 3
  • 45
  • 56
user2820823
  • 86
  • 1
  • 1
  • 9
  • 1
    Show us the code and log please. – luiscosta Jul 24 '14 at 09:25
  • ok wait... i'll post the code. – user2820823 Jul 24 '14 at 09:26
  • You shouldn't be doing a db insert every time you find a file. Instead, save the info in a data structure, like a List, then dump it to the db when the scan is complete. – Mike M. Jul 24 '14 at 09:35
  • @MikeM. how can we do that? – user2820823 Jul 24 '14 at 09:37
  • @vokilam thnx for the edit – user2820823 Jul 24 '14 at 09:38
  • 1
    Create a POJO class with only the file data you need. Create a List of those objects. Instead of calling `DBoperation(val)`, add an instance of your POJO for each file to the List. When it's done scanning, insert the List's data into the db. [Check this post](http://stackoverflow.com/questions/11328350/how-to-speed-up-large-data-insertion-in-database-sqlite-in-android-application) for tips on inserting large amounts of data. – Mike M. Jul 24 '14 at 10:00
  • I forgot to mention, an `AsyncTask` might not be the best solution if you're traversing a sizable hierarchy, as they are meant for short-lived operations. You might look into `Service`s, specifically `IntentService`s. – Mike M. Jul 24 '14 at 10:20

1 Answers1

0

Check your logcat it should tell you where the problem occurs. Make sure the objects you use are not null. For instance, the 'params'.

Another possibility file.listFiles() will return null if that file is empty, you'll get NPE when try to use it

Guster
  • 1,733
  • 1
  • 16
  • 18