List of usage examples for android.content.res AssetManager open
public @NonNull InputStream open(@NonNull String fileName) 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); }/* ww w . jav a2 s .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 . java2 s .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; }
From source file:processing.core.PApplet.java
/** * Call createInput() without automatic gzip decompression. *//*from w w w . j av a 2 s. c o m*/ public InputStream createInputRaw(String filename) { // Additional considerations for Android version: // http://developer.android.com/guide/topics/resources/resources-i18n.html InputStream stream = null; if (filename == null) return null; if (filename.length() == 0) { // an error will be called by the parent function // System.err.println("The filename passed to openStream() was empty."); return null; } // safe to check for this as a url first. this will prevent online // access logs from being spammed with GET /sketchfolder/http://blahblah if (filename.indexOf(":") != -1) { // at least smells like URL try { // Workaround for Android bug 6066 // http://code.google.com/p/android/issues/detail?id=6066 // http://code.google.com/p/processing/issues/detail?id=629 // URL url = new URL(filename); // stream = url.openStream(); // return stream; HttpGet httpRequest = null; httpRequest = new HttpGet(URI.create(filename)); HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = (HttpResponse) httpclient.execute(httpRequest); HttpEntity entity = response.getEntity(); return entity.getContent(); // can't use BufferedHttpEntity because it may try to allocate a // byte // buffer of the size of the download, bad when DL is 25 MB... // [0200] // BufferedHttpEntity bufHttpEntity = new // BufferedHttpEntity(entity); // return bufHttpEntity.getContent(); } catch (MalformedURLException mfue) { // not a url, that's fine } catch (FileNotFoundException fnfe) { // Java 1.5 likes to throw this when URL not available. (fix for // 0119) // http://dev.processing.org/bugs/show_bug.cgi?id=403 } catch (IOException e) { // changed for 0117, shouldn't be throwing exception e.printStackTrace(); // System.err.println("Error downloading from URL " + filename); return null; // throw new RuntimeException("Error downloading from URL " + // filename); } } /* * // Moved this earlier than the getResourceAsStream() checks, because * // calling getResourceAsStream() on a directory lists its contents. * // http://dev.processing.org/bugs/show_bug.cgi?id=716 try { // First * see if it's in a data folder. This may fail by throwing // a * SecurityException. If so, this whole block will be skipped. File file * = new File(dataPath(filename)); if (!file.exists()) { // next see if * it's just in the sketch folder file = new File(sketchPath, filename); * } if (file.isDirectory()) { return null; } if (file.exists()) { try { * // handle case sensitivity check String filePath = * file.getCanonicalPath(); String filenameActual = new * File(filePath).getName(); // make sure there isn't a subfolder * prepended to the name String filenameShort = new * File(filename).getName(); // if the actual filename is the same, but * capitalized // differently, warn the user. //if * (filenameActual.equalsIgnoreCase(filenameShort) && * //!filenameActual.equals(filenameShort)) { if * (!filenameActual.equals(filenameShort)) { throw new * RuntimeException("This file is named " + filenameActual + " not " + * filename + ". Rename the file " + "or change your code."); } } catch * (IOException e) { } } * * // if this file is ok, may as well just load it stream = new * FileInputStream(file); if (stream != null) return stream; * * // have to break these out because a general Exception might // catch * the RuntimeException being thrown above } catch (IOException ioe) { } * catch (SecurityException se) { } */ // Using getClassLoader() prevents Java from converting dots // to slashes or requiring a slash at the beginning. // (a slash as a prefix means that it'll load from the root of // the jar, rather than trying to dig into the package location) /* * // this works, but requires files to be stored in the src folder * ClassLoader cl = getClass().getClassLoader(); stream = * cl.getResourceAsStream(filename); if (stream != null) { String cn = * stream.getClass().getName(); // this is an irritation of sun's java * plug-in, which will return // a non-null stream for an object that * doesn't exist. like all good // things, this is probably introduced * in java 1.5. awesome! // * http://dev.processing.org/bugs/show_bug.cgi?id=359 if * (!cn.equals("sun.plugin.cache.EmptyInputStream")) { return stream; } * } */ // Try the assets folder AssetManager assets = this.getActivity().getAssets(); try { stream = assets.open(filename); if (stream != null) { return stream; } } catch (IOException e) { // ignore this and move on // e.printStackTrace(); } // Maybe this is an absolute path, didja ever think of that? File absFile = new File(filename); if (absFile.exists()) { try { stream = new FileInputStream(absFile); if (stream != null) { return stream; } } catch (FileNotFoundException fnfe) { // fnfe.printStackTrace(); } } // Maybe this is a file that was written by the sketch on another // occasion. File sketchFile = new File(sketchPath(filename)); if (sketchFile.exists()) { try { stream = new FileInputStream(sketchFile); if (stream != null) { return stream; } } catch (FileNotFoundException fnfe) { // fnfe.printStackTrace(); } } // Attempt to load the file more directly. Doesn't like paths. Context context = this.getActivity().getApplicationContext(); try { // MODE_PRIVATE is default, should we use something else? stream = context.openFileInput(filename); if (stream != null) { return stream; } } catch (FileNotFoundException e) { // ignore this and move on // e.printStackTrace(); } return null; }
From source file:com.processing.core.PApplet.java
/** * Call createInput() without automatic gzip decompression. *//*from ww w. j a va 2 s . co m*/ public InputStream createInputRaw(String filename) { // Additional considerations for Android version: // http://developer.android.com/guide/topics/resources/resources-i18n.html InputStream stream = null; if (filename == null) return null; if (filename.length() == 0) { // an error will be called by the parent function //System.err.println("The filename passed to openStream() was empty."); return null; } // safe to check for this as a url first. this will prevent online // access logs from being spammed with GET /sketchfolder/http://blahblah if (filename.indexOf(":") != -1) { // at least smells like URL try { // Workaround for Android bug 6066 // http://code.google.com/p/android/issues/detail?id=6066 // http://code.google.com/p/processing/issues/detail?id=629 // URL url = new URL(filename); // stream = url.openStream(); // return stream; HttpGet httpRequest = null; httpRequest = new HttpGet(URI.create(filename)); HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = (HttpResponse) httpclient.execute(httpRequest); HttpEntity entity = response.getEntity(); return entity.getContent(); // can't use BufferedHttpEntity because it may try to allocate a byte // buffer of the size of the download, bad when DL is 25 MB... [0200] // BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); // return bufHttpEntity.getContent(); } catch (MalformedURLException mfue) { // not a url, that's fine } catch (FileNotFoundException fnfe) { // Java 1.5 likes to throw this when URL not available. (fix for 0119) // http://dev.processing.org/bugs/show_bug.cgi?id=403 } catch (IOException e) { // changed for 0117, shouldn't be throwing exception e.printStackTrace(); //System.err.println("Error downloading from URL " + filename); return null; //throw new RuntimeException("Error downloading from URL " + filename); } } /* // Moved this earlier than the getResourceAsStream() checks, because // calling getResourceAsStream() on a directory lists its contents. // http://dev.processing.org/bugs/show_bug.cgi?id=716 try { // First see if it's in a data folder. This may fail by throwing // a SecurityException. If so, this whole block will be skipped. File file = new File(dataPath(filename)); if (!file.exists()) { // next see if it's just in the sketch folder file = new File(sketchPath, filename); } if (file.isDirectory()) { return null; } if (file.exists()) { try { // handle case sensitivity check String filePath = file.getCanonicalPath(); String filenameActual = new File(filePath).getName(); // make sure there isn't a subfolder prepended to the name String filenameShort = new File(filename).getName(); // if the actual filename is the same, but capitalized // differently, warn the user. //if (filenameActual.equalsIgnoreCase(filenameShort) && //!filenameActual.equals(filenameShort)) { if (!filenameActual.equals(filenameShort)) { throw new RuntimeException("This file is named " + filenameActual + " not " + filename + ". Rename the file " + "or change your code."); } } catch (IOException e) { } } // if this file is ok, may as well just load it stream = new FileInputStream(file); if (stream != null) return stream; // have to break these out because a general Exception might // catch the RuntimeException being thrown above } catch (IOException ioe) { } catch (SecurityException se) { } */ // Using getClassLoader() prevents Java from converting dots // to slashes or requiring a slash at the beginning. // (a slash as a prefix means that it'll load from the root of // the jar, rather than trying to dig into the package location) /* // this works, but requires files to be stored in the src folder ClassLoader cl = getClass().getClassLoader(); stream = cl.getResourceAsStream(filename); if (stream != null) { String cn = stream.getClass().getName(); // this is an irritation of sun's java plug-in, which will return // a non-null stream for an object that doesn't exist. like all good // things, this is probably introduced in java 1.5. awesome! // http://dev.processing.org/bugs/show_bug.cgi?id=359 if (!cn.equals("sun.plugin.cache.EmptyInputStream")) { return stream; } } */ // Try the assets folder AssetManager assets = getAssets(); try { stream = assets.open(filename); if (stream != null) { return stream; } } catch (IOException e) { // ignore this and move on //e.printStackTrace(); } // Maybe this is an absolute path, didja ever think of that? File absFile = new File(filename); if (absFile.exists()) { try { stream = new FileInputStream(absFile); if (stream != null) { return stream; } } catch (FileNotFoundException fnfe) { //fnfe.printStackTrace(); } } // Maybe this is a file that was written by the sketch on another occasion. File sketchFile = new File(sketchPath(filename)); if (sketchFile.exists()) { try { stream = new FileInputStream(sketchFile); if (stream != null) { return stream; } } catch (FileNotFoundException fnfe) { //fnfe.printStackTrace(); } } // Attempt to load the file more directly. Doesn't like paths. Context context = getApplicationContext(); try { // MODE_PRIVATE is default, should we use something else? stream = context.openFileInput(filename); if (stream != null) { return stream; } } catch (FileNotFoundException e) { // ignore this and move on //e.printStackTrace(); } return null; }