Copy file from assets to folder on the sdcard fails

440 Views Asked by At

I am using tesseract OCR in my app (Spl!t). When I launch the app from Eclipse to my phone, eng.traineddata is copied into /storage/emulated/0/Pictures/Receipts/tessdata/. But when I installed the app from the market, the eng.traineddata file is not copied into the folder. Is there something wrong with my code?

  private File getOutputPhotoFile() {
      directory = new File(
              Environment
                      .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
              "Receipts");
      dataPath = directory.getAbsolutePath();
      if (!directory.exists()) {
          directory.mkdirs();
          Toast.makeText(Trans_List.this, "Receipts Folder Created", Toast.LENGTH_SHORT).show();
          File tessDir = new File(directory, "tessdata");
          if (!tessDir.exists()) {
              tessDir.mkdirs();
          Toast.makeText(Trans_List.this, "Tessdata Folder Created",  Toast.LENGTH_SHORT).show();
              File trainingData = new File(tessDir, "eng.traineddata");
              if(!trainingData.exists())
                  new CopyLibrary().execute(LANG);
          }
      }
      String timeStamp = new SimpleDateFormat("yyyMMdd_HHmmss")
              .format(new Date());
      return new File(directory.getPath() + File.separator + "Receipt_"
              + timeStamp + ".jpg");
  }   

private class CopyLibrary extends AsyncTask<String, Void, Void> {

  private void copyFile(InputStream in, OutputStream out)
          throws IOException {
      byte[] buffer = new byte[1024];
      int read;
      while ((read = in.read(buffer)) != -1)
          out.write(buffer, 0, read);
  }

  @Override
  protected Void doInBackground(String... s) {
      AssetManager assetManager = getAssets();
      String[] files = null;
      try {
          files = assetManager.list("");
      } catch (IOException e) {
          Log.e("Spl!t", "Failed to get asset file list.");
      }
      for (String filename : files) {
      InputStream in = null;
          OutputStream out = null;
          try {
              in = assetManager.open(filename);
              File outFile = new File(dataPath + File.separator
                      + "tessdata" + File.separator, LANG
                      + ".traineddata");
              out = new FileOutputStream(outFile);
              copyFile(in, out);
              in.close();
              in = null;
              out.flush();
              out.close();
              Toast.makeText(Trans_List.this, "Tessdata copied", Toast.LENGTH_SHORT).show();
              out = null;
          } catch (IOException e) {
              Log.e(TAG, "Failed to copy asset file");
          }
      }
      return null;
  }
}
0

There are 0 best solutions below