Copying file from assets directory to SDCard in Android

File file = new File(getExternalCacheDir(), "filename.ext" ); if (file.exists()) { System.out.println("File Exist"); } else { System.out.println("File NOT Exist"); } This is the method for copying file from assets to SD card private void copyAssets() { AssetManager assetManager = getAssets(); String[] files = null; InputStream in = null; OutputStream out = null; String filename = "filename.ext"; try { in = assetManager.open(filename); out = new FileOutputStream("/sdcard/" + filename); copyFile(in, out); in.close(); in = null; out.flush(); out.close(); out = null; } catch(IOException e) { Log.e("tag", "Failed to copy asset file: " + filename, e); } } 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); } }
This snippet show to to copy file from assets directory to SDCard in Android

#assets #file #SDCard #android

#cesarnog

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.