public static String loadJSONFromAsset(Context context, String jsonFileName) throws IOException { AssetManager manager = context.getAssets(); InputStream is = manager.open(jsonFileName); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); return new String(buffer, "UTF-8"); }
private static void copyFile(String assetPath, String localPath, Context context) { try { InputStream in = context.getAssets().open(assetPath); FileOutputStream out = new FileOutputStream(localPath); int read; byte[] buffer = new byte[4096]; while ((read = in.read(buffer)) > 0) { out.write(buffer, 0, read); } out.close(); in.close(); } catch (IOException e) { throw new RuntimeException(e); } }
File Path = Ctxt.getDir("Data", 0); File DBFile = new File(Path, "database.db"); if(!DBFile.exists() || DatabaseNeedsUpgrade) //Need to copy... CopyDatabase(Ctxt, DBFile); static private void CopyDatabase(Context Ctxt, File DBFile) throws IOException { AssetManager assets = Ctxt.getAssets(); OutputStream outstream = new FileOutputStream(DBFile); DBFile.createNewFile(); byte []b = new byte[1024]; int i, r; String []assetfiles = assets.list(""); Arrays.sort(assetfiles); for(i=1;i<10;i++) //I have definitely less than 10 files; you might have more { String partname = String.format("%d.db", i); if(Arrays.binarySearch(assetfiles, partname) < 0) //No such file in assets - time to quit the loop break; InputStream instream = assets.open(partname); while((r = instream.read(b)) != -1) outstream.write(b, 0, r); instream.close(); } outstream.close(); }
public static String copyAsset(Context context, String assetName, File dir) throws IOException { File outFile = new File(dir, assetName); if (!outFile.exists()) { AssetManager assetManager = context.getAssets(); InputStream in = assetManager.open(assetName); OutputStream out = new FileOutputStream(outFile); copyFile(in, out); in.close(); out.close(); } return outFile.getAbsolutePath(); }
private void copyFromAsset(Context ct, String fileName, String targetPath) { try (InputStream in = ct.getAssets().open(fileName); OutputStream out = new FileOutputStream(targetPath)) { byte[] buffer = new byte[1024]; int read; while((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); } } catch(IOException e) { Log.e("tag", "Failed to copy asset file: ", e); } }
public static String getXMLFromAssets(Context ctx, String pathToXML){ InputStream rawInput; //create a output stream to write the buffer into ByteArrayOutputStream rawOutput = null; try { rawInput = ctx.getAssets().open(pathToXML); //create a buffer that has the same size as the InputStream byte[] buffer = new byte[rawInput.available()]; //read the text file as a stream, into the buffer rawInput.read(buffer); rawOutput = new ByteArrayOutputStream(); //write this buffer to the output stream rawOutput.write(buffer); //Close the Input and Output streams rawOutput.close(); rawInput.close(); } catch (IOException e) { Log.e("Error", e.toString()); } //return the output stream as a String return rawOutput.toString(); }
public static void copyDex(Context context, String dexName) { File dexInternalStoragePath = new File(context.getDir("dex", Context.MODE_PRIVATE), dexName); BufferedInputStream bis = null; OutputStream dexWriter = null; try { bis = new BufferedInputStream(context.getAssets().open(dexName)); dexWriter = new BufferedOutputStream( new FileOutputStream(dexInternalStoragePath)); byte[] buf = new byte[BUF_SIZE]; int len; while((len = bis.read(buf, 0, BUF_SIZE)) > 0) { dexWriter.write(buf, 0, len); } dexWriter.close(); bis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
/** * 把Assets里面得文件复制到 /data/data/files 目录下 * * @param context * @param sourceName */ public static void extractAssets(Context context, String sourceName) { AssetManager am = context.getAssets(); InputStream is = null; FileOutputStream fos = null; try { is = am.open(sourceName); File extractFile = context.getFileStreamPath(sourceName); fos = new FileOutputStream(extractFile); byte[] buffer = new byte[1024]; int count = 0; while ((count = is.read(buffer)) > 0) { fos.write(buffer, 0, count); } fos.flush(); } catch (IOException e) { e.printStackTrace(); } finally { closeSilently(is); closeSilently(fos); } }
InputStream mInput = mContext.getAssets().open(DB_NAME); String outFileName = DB_PATH + DB_NAME; OutputStream mOutput = new FileOutputStream(outFileName); byte[] mBuffer = new byte[1024]; int mLength; while ((mLength = mInput.read(mBuffer))>0) mOutput.write(mBuffer, 0, mLength); mOutput.flush(); mOutput.close(); mInput.close();
private static String loadJson(Context context) { String jsonString; try { InputStream is = context.getAssets().open(COLORS_FILE_NAME); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); jsonString = new String(buffer, "UTF-8"); } catch (IOException ex) { ex.printStackTrace(); return null; } return jsonString; }
private static CityDB openCityDB(Context context, String packageName) { String path = "/data" + Environment.getDataDirectory().getAbsolutePath() + File.separator + packageName + File.separator + CityDB.CITY_DB_NAME; File db = new File(path); if (!db.exists()) { try { InputStream is = context.getAssets().open("city.db"); FileOutputStream fos = new FileOutputStream(db); int len = -1; byte[] buffer = new byte[1024]; while ((len = is.read(buffer)) != -1) { fos.write(buffer, 0, len); fos.flush(); } fos.close(); is.close(); } catch (IOException e) { e.printStackTrace(); System.exit(0); } } return new CityDB(context, path); }
public static boolean ExtractToAppCache(Context context, String fileAsset, String output) { Log.i(LOGTAG, "Starting extraction of asset file \"" + fileAsset + "\"..."); Log.i(LOGTAG, "The file will be extracted as \"" + context.getCacheDir() + "/" + output + "\""); try { InputStream in = context.getAssets().open(fileAsset); OutputStream out = new FileOutputStream(new File(context.getCacheDir() + "/" + output)); try { copyFile(in, out); in.close(); out.flush(); out.close(); Log.i(LOGTAG, "Successfully extracted asset file!"); return true; } catch (IOException e) { OutputStream outputStream = out; Log.e(LOGTAG, "Failed to extract asset file!"); return false; } } catch (IOException e2) { Log.e(LOGTAG, "Failed to extract asset file!"); return false; } }
in = new BufferedInputStream(context.getAssets().open(fileName)); out = new BufferedOutputStream(new FileOutputStream(file)); while ((count = in.read(buffer)) != -1) { out.write(buffer, 0, count); out.flush();
/** * 把Assets里面得文件复制到 /data/data/files 目录下 * * @param context * @param sourceName */ public static void extractAssets(Context context, String sourceName) { AssetManager am = context.getAssets(); InputStream is = null; FileOutputStream fos = null; try { is = am.open(sourceName); File extractFile = context.getFileStreamPath(sourceName); fos = new FileOutputStream(extractFile); byte[] buffer = new byte[1024]; int count = 0; while ((count = is.read(buffer)) > 0) { fos.write(buffer, 0, count); } fos.flush(); } catch (IOException e) { e.printStackTrace(); } finally { closeSilently(is); closeSilently(fos); } }
private static String loadJSONFromAsset(Context context, String jsonFileName) { String json = null; InputStream is=null; try { AssetManager manager = context.getAssets(); Log.d(TAG,"path "+jsonFileName); is = manager.open(jsonFileName); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); json = new String(buffer, "UTF-8"); } catch (IOException ex) { ex.printStackTrace(); return null; } return json; }
private static CityDB openCityDB(Context context, String packageName) { String path = "/data" + Environment.getDataDirectory().getAbsolutePath() + File.separator + packageName + File.separator + CityDB.CITY_DB_NAME; File db = new File(path); if (!db.exists()) { try { InputStream is = context.getAssets().open("city.db"); FileOutputStream fos = new FileOutputStream(db); int len = -1; byte[] buffer = new byte[1024]; while ((len = is.read(buffer)) != -1) { fos.write(buffer, 0, len); fos.flush(); } fos.close(); is.close(); } catch (IOException e) { e.printStackTrace(); System.exit(0); } } return new CityDB(context, path); }
/** * @param context Application context * @param assetFileName Name of the file stored in assets * @param destinationDirectory Destination folder for saving the file * @brief Copies a file from assets folder to a folder on device memory */ public static void copyAssets(Context context, String assetFileName, String destinationDirectory) { AssetManager assetManager = context.getAssets(); InputStream in; OutputStream out; try { in = assetManager.open(assetFileName); File f = new File(destinationDirectory); if (!f.isDirectory()) { f.mkdirs(); } File outFile = new File(destinationDirectory, assetFileName); out = new FileOutputStream(outFile); copyFile(in, out); in.close(); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } }
AssetManager assetManager = c.getAssets(); InputStream in = null; OutputStream out = null; in = assetManager.open(rootAsset + path); String fullPath = PrefStore.getEnvDir(c) + path; out = new FileOutputStream(fullPath); byte[] buffer = new byte[1024]; int read; while ((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); out.flush(); } catch (IOException e) { e.printStackTrace();
InputStream myInput = dbContext.getAssets().open(DATABASE_NAME); String outFileName = DATABASE_PATH + DATABASE_NAME; OutputStream myOutput = new FileOutputStream(outFileName); while ((length = myInput.read(buffer))>0){ myOutput.write(buffer, 0, length); myOutput.flush(); myOutput.close(); myInput.close();