Copy file from asset to sdcard in android

Copy file Asset to sd card android

In some apps need to move some files from asset to sd card or phone memory for feature use. This tutorial helps you to move the file from asset to sd card or phone memory.
Follow the below steps to copy file Asset  to sd card:
1. First, get the files from the asset, so that you have to create the AssetManager object.
AssetManager assetManager = getAssets();

2. get the file from the asset folder using asset manager.

String[] files;
files = assetManager.list("Files");

3. Create input & output stream for set input and output file locations.

InputStream in = null;
OutputStream out = null;

4. Finally, copy the file to sd card using input and output locations using CopyFile() method.

in = assetManager.open("Files/"+filename);
out = new FileOutputStream(Environment.getExternalStorageDirectory().toString() +"/" + filename);
copyFile(in, out);

Here, CopyFile method read all content of the file and write the data into the destination location.

private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = newbyte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
Download

Leave a Reply

Your email address will not be published. Required fields are marked *