1

I want to load a model file that will be present in the user's external storage. Size of the model file can be upto 20MB or so. It contains x,y,z info separated by '\n'. When i try to load a small file by my method it runs fine. But for a large file the application asks for a force quit.

File dir = Environment.getExternalStorageDirectory();
        File file = new File(dir,"model_Cube.txt");
        BufferedReader reader = null;
        List<Float> list = new ArrayList<Float>();

        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;
            while ((text = reader.readLine()) != null) {
                list.add(Float.parseFloat(text));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
        try {
            reader.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            reader.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        triangleCoords = new float[list.size()];

        for (int i = 0; i < list.size(); i++) {
            Float f = list.get(i);
            triangleCoords[i] = (float) (f.floatValue()/10.0); // Or whatever default you want.
        }
fight2glory
  • 57
  • 2
  • 9
  • ...So why do the files need to be that big for the operations you're performing? Wouldn't it be better (if the data got too intense) to store that in SQLite instead? – Makoto Jun 15 '13 at 05:33
  • Well it contains a pointcloud information of a 3D-model say(Taj mahal). right now whati want to do is read the point cloud info and view it. and for creating the database i still need to read the file first don't i? – fight2glory Jun 15 '13 at 05:38
  • @Makoto I think SQLite BLOB in Android are limited to 1MB. – Alexandre Lavoie Jun 15 '13 at 05:52
  • I have provided the solution here using scanner https://stackoverflow.com/a/54961878/7680587 – Prasanth.NVS Mar 02 '19 at 20:22

2 Answers2

7

The problem here is not in your code works, it is actually a problem with threading.

This is happening because the device sees that you are doing a large process in the main user interface thread. Look into Asynchronous Tasks. http://developer.android.com/reference/android/os/AsyncTask.html

This is how Android handles multi-threading in applications. Performing large file loads or calculations in the main thread may cause the app to become unresponsive and the operating system will think that it has crashed.

Splitting this functionality into a separate thread will avoid the issue.

Here is a decent tutorial with a sample implementation of an Asynchronous Task. http://androidresearch.wordpress.com/2012/03/17/understanding-asynctask-once-and-forever/

JFeather
  • 156
  • 2
3

You should use AsyncTask (http://developer.android.com/reference/android/os/AsyncTask.html) or separate Thread (http://developer.android.com/reference/java/lang/Thread.html) for reading this file. Now you are reading this file in main thread (UI thread) and while it reading, UI not responding. Also, please note that may be better to use SQLite instead of such big files.

Dimmerg
  • 2,113
  • 1
  • 13
  • 14