将文件从资产复制到SD卡上的文件夹失败

时间:2014-11-14 22:09:19

标签: java android android-asynctask tesseract mkdir

我在我的应用程序中使用tesseract OCR(Spl!t)。当我从Eclipse启动应用程序到我的手机时,eng.traineddata被复制到/ storage / emulated / 0 / Pictures / Receipts / tessdata /。但是当我从市场上安装应用程序时,eng.traineddata文件不会复制到该文件夹​​中。我的代码有问题吗?

  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 个答案:

没有答案