List of usage examples for android.content.res AssetManager list
public @Nullable String[] list(@NonNull String path) throws IOException
From source file:org.de.jmg.learn.MainActivity.java
private void CopyAssets() { libLearn.gStatus = "Copy Assets"; File F = android.os.Environment.getExternalStorageDirectory(); boolean successful = false; for (int i = 0; i < 2; i++) { if (!F.exists() || i == 1) { F = new File(getApplicationInfo().dataDir); }/* w w w . j a v a2s . c o m*/ String extPath = F.getPath(); JMGDataDirectory = prefs.getString("JMGDataDirectory", Path.combine(extPath, "learnforandroid", "vok")); //JMGDataDirectory = Path.combine(extPath, "learnforandroid", "vok"); if (F.isDirectory() && F.exists()) { File F1 = new File(JMGDataDirectory); if (!F1.isDirectory() && !F1.exists()) { System.out.println(F1.mkdirs()); } else { int dontcopy = prefs.getInt("dontcopyorchoose", -2); yesnoundefined rres; if (dontcopy == -2) { rres = yesnoundefined.undefined; } else if (dontcopy == 0) { rres = yesnoundefined.yes; } else { rres = yesnoundefined.no; } lib.YesNoCheckResult res = new lib.YesNoCheckResult(rres, false); if (rres == yesnoundefined.undefined) { res = lib.ShowMessageYesNoWithCheckbox(this, this.getString(R.string.copy), this.getString(R.string.copyfiles), this.getString(R.string.msgRememberChoice), false); } if (res != null) { if (res.checked) { prefs.edit().putInt("dontcopyorchoose", (res.res == yesnoundefined.no ? -1 : 0)) .commit(); } if (res.res == yesnoundefined.no) return; } } AssetManager A = this.getAssets(); try { final String languages[] = new String[] { "Greek", "Hebrew", "KAR", "Spanish" }; final String path = F1.getPath(); for (String language : languages) { F1 = new File(Path.combine(path, language)); for (String File : A.list(language)) { InputStream myInput = A.open(Path.combine(language, File)); String outFileName = Path.combine(F1.getPath(), File); if (!F1.isDirectory()) { System.out.println(F1.mkdirs()); } // Open the empty db as the output stream File file = new File(outFileName); if (file.exists()) { // file.delete(); successful = true; } else { try { System.out.println(file.createNewFile()); OutputStream myOutput = new FileOutputStream(file); byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer, 0, 1024)) > 0) { myOutput.write(buffer, 0, length); } // Close the streams myOutput.flush(); myOutput.close(); myInput.close(); successful = true; } catch (Exception eex) { if (i == 0) throw eex; lib.ShowMessage(this, this.getString(R.string.fileCouldNotBeCreated) + " " + outFileName, this.getString(R.string.Error)); //successful=false; throw eex; } } } } } catch (IOException e) { // e.printStackTrace(); if (i > 0) lib.ShowException(this, e); // lib.ShowMessage(this, "CopyAssets"); successful = false; } } if (successful) break; } }
From source file:net.robotmedia.acv.ui.widget.OcrLayout.java
/** Copy OCR assets to the application folder in external storage. * http://stackoverflow.com/questions/4447477/android-how-to-copy-files-in-assets-to-sdcard * *//*from w w w . ja v a 2s .c o m*/ private boolean copyOcrAssets() { AssetManager assetManager = this.context.getAssets(); ContextWrapper cw = new ContextWrapper(this.context); String[] files = null; File externalDir = this.context.getExternalFilesDir(null); File tessdataDir = new File(externalDir.getPath() + Constants.TESSDATA_DIR); // Create the Tesseract directory structure if it doesn't exist if (!tessdataDir.exists()) { tessdataDir.mkdirs(); } try { files = assetManager.list(""); } catch (IOException e) { Log.e(LOG_TAG, "copyOcrAssets() failed to get asset file list!" + e); return false; } for (String filename : files) { InputStream in = null; OutputStream out = null; // Only store expected files if (filename.equals(Constants.EDICT_DB_FILENAME) || filename.equals(Constants.TESSERACT_DB_FILENAME) || filename.equals(Constants.DEINFLECTION_DB_FILENAME) || filename.equals(Constants.SUBSTITUTIONS_DB_FILENAME) || filename.equals(Constants.EPLKUP_FILENAME) || filename.equals(Constants.EPLKUP_NON_PIE_FILENAME) || filename.equals(Constants.FREQ_DB_FILENAME)) { try { in = assetManager.open(filename); File outFile; if (filename.equals(Constants.TESSERACT_DB_FILENAME)) { outFile = new File(tessdataDir, filename); } else if (filename.equals(Constants.EPLKUP_FILENAME)) { outFile = new File(cw.getApplicationInfo().dataDir + "/" + Constants.EPLKUP_FILENAME); } else if (filename.equals(Constants.EPLKUP_NON_PIE_FILENAME)) { outFile = new File( cw.getApplicationInfo().dataDir + "/" + Constants.EPLKUP_NON_PIE_FILENAME); } else { outFile = new File(externalDir, filename); } out = new FileOutputStream(outFile); FileUtils.copyFile(in, out); in.close(); in = null; out.flush(); out.close(); out = null; // Give eplkup execute permissions if (filename.equals(Constants.EPLKUP_FILENAME)) { ShellUtils.chmod(cw.getApplicationInfo().dataDir + "/" + Constants.EPLKUP_FILENAME, "744"); } else if (filename.equals(Constants.EPLKUP_NON_PIE_FILENAME)) { ShellUtils.chmod(cw.getApplicationInfo().dataDir + "/" + Constants.EPLKUP_NON_PIE_FILENAME, "744"); } } catch (IOException e) { Log.e(LOG_TAG, "copyOcrAssets() copy failed!" + e); return false; } } } return true; }