2017-07-31 8 views
0

私は、いくつかのtxtファイルを読み込み、デバイス上のSQLiteデータベースにインポートするための小さなアプリケーションを作っています。
問題は、ファイルを読み込んだり読み込んだりするときに、メソッドを実行しているときにアプリがフリーズしてしまうことです。そして、私は進行状況バーなどを表示したいが、この問題では何もできない。
これは、インポートのメソッドのコードです:あなたはデシベルで複数の操作を行っている場合は、より良いあなたが別のスレッドで実行しても、トランザクションを使用しようとすべきであるであるあなた
アプリケーションがフリーズ中に挿入する(Android SQLite DB)

private void importFilesFromTxt() { 
    bd = helper.getWritableDatabase(); 
    File directorio = getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS); 
    File file = new File(directorio, "art01.txt"); 
    try { 
     BufferedReader br = new BufferedReader(new FileReader(file)); 
     String line; 
     String[] parts; 
     String sql = "delete from Articulos"; 
     bd.execSQL(sql); 
     while ((line = br.readLine()) != null) { 
      if (line.length() != 328) { 
       parts = line.split("\\#+"); 
       Articulo arti = new Articulo(Integer.parseInt(parts[0]), parts[1], quitarEspaciosInt(parts[2]), quitarEspaciosInt(parts[3]) 
         , convertirDecimal(parts[4]), quitarEspaciosInt(parts[5]), quitarEspaciosInt(parts[6]), quitarEspaciosFloat(parts[7]), quitarEspaciosInt(parts[8])); 
       helper.addArticulo(arti); 
      } 
     } 
     bd.close(); 
     br.close(); 
    } catch (IOException e) { 
     System.out.println(e.toString()); 
    } 
} 
+0

アプリがフリーズすると、エラーログがログcatに表示されます。あなたはそれを追加することができます –

+0

それは私にエラーを与えていないことです。 「1106フレームをスキップしました!アプリケーションがメインスレッドで処理しすぎている可能性があります」 – ZamoraFTW

+0

次に、メインスレッドであまりにも多くの操作をしているので、バックグラウンドスレッドで実行する必要があります。あなたのtxtファイルの大きさ –

答えて

4

は、これを試してみてください

 new AsyncTask<Void, Void, Void>() { 

      @Override 
      protected Void doInBackground(Void... voids) { 
       bd = helper.getWritableDatabase(); 
       File directorio = getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS); 
       File file = new File(directorio, "art01.txt"); 
       try { 
        BufferedReader br = new BufferedReader(new FileReader(file)); 
        String line; 
        String[] parts; 
        String sql = "delete from Articulos"; 
        bd.execSQL(sql); 

        bd.beginTransaction(); 

        while ((line = br.readLine()) != null) { 
         if (line.length() != 328) { 
          parts = line.split("\\#+"); 
          Articulo arti = new Articulo(Integer.parseInt(parts[0]), parts[1], quitarEspaciosInt(parts[2]), quitarEspaciosInt(parts[3]) 
            , convertirDecimal(parts[4]), quitarEspaciosInt(parts[5]), quitarEspaciosInt(parts[6]), quitarEspaciosFloat(parts[7]), quitarEspaciosInt(parts[8])); 
          helper.addArticulo(arti); 
         } 
        } 

        br.close(); 
        bd.setTransactionSuccessful(); 
       } catch (IOException e) { 
//     System.out.println(e.toString()); 
       } catch (Exception e) { 

       } finally { 
        bd.endTransaction(); 
        bd.close(); 
       } 
       return null; 
      } 
     }; 
関連する問題